問題描述
我有一個獨特的問題.我正在使用他們無法支持請求的插件.我需要將變體拆分為單獨的項目,但是如果我復(fù)制和粘貼并將它們變成一個簡單的產(chǎn)品,那么我無法同步產(chǎn)品的計數(shù)以跟蹤庫存.作為一種解決方法,我需要能夠禁用不需要的變體,只保留我確實需要的變體.但這是我遇到麻煩的地方.如果我啟用了一個變體,那么我不想顯示下拉列表,而是希望它在 UI 上看起來像一個簡單的產(chǎn)品.我嘗試了一切,但無法讓它工作.
I have a unique issue. I am using a plugin where they are not able to support the request. I need to split out variations into separate items, but if I copy and paste and turn them into a simple product, then I can't sync the count for the product to track inventory stock. As a workaround I needed to be able to disable the variations I do not need, keeping only the one that I do need. But here is where I am having trouble. If I have one variation enabled, then I do not want to show the dropdown, and instead want it to look like a simple product on the UI. I tried everything and cannot get it to work.
我什至嘗試使用
add_filter( 'woocommerce_hide_invisible_variations', '__return_true', 10, 3 );
沒有成功,因為即使計數(shù)為 0、價格為 0 且項目已禁用,它們?nèi)钥梢娗椅措[藏.
with no success as they are visible and not hidden even though the counts are 0, the price is 0, and the item is disabled.
如何顯示沒有下拉菜單的產(chǎn)品頁面?更進一步;我刪除了所有變體,但我需要保留的變體除外.由于同步插件,我需要將其保持在變化模式.如何在不顯示任何下拉列表的情況下顯示它?
How can I show the product page with no drop-down? Take it one step further; I delete all variations except the one that I need to keep. I need to keep it in variations mode due to the plugin that syncs. How do I display it without any dropdowns showing?
示例邏輯:
如果產(chǎn)品類型是變體并且啟用計數(shù) == 1 則特殊 ui 顯示,否則正常.
If product type is a variation and enabled count == 1 then special ui display, else normal.
謝謝.
推薦答案
重要提示:代碼僅適用當(dāng)唯一變體被選擇為默認(rèn)表單值時:
IMPORTANT: The code only works when the unique variation is selected as default form value:
以下代碼將隱藏僅啟用一個變體并默認(rèn)選擇的變量產(chǎn)品、屬性下拉列表和所選變體價格:
The following code will hide from variable products that have only one variation enabled and selected by default, the attribute dropdown and the selected variation price:
add_action( 'woocommerce_before_add_to_cart_form', 'single_unique_variation_ui', 10 );
function single_unique_variation_ui(){
global $product;
if( ! $product->is_type('variable') )
return; // Only variable products
$available_variations = $product->get_available_variations(); // Get available variations
$default_attributes = $product->get_default_attributes(); // Get default attributes
// Only for a unique selected variation by default
if( ! ( sizeof($available_variations) == 1 && sizeof($default_attributes) == 1 ) )
return;
// Get the unique variation
$variation = reset($available_variations);
// Loop through
if( reset($variation['attributes']) == reset($default_attributes) ) :
// Styles
?>
<style>
div.woocommerce-variation-price, table.variations { display:none; }
</style>
<?php
endif;
}
代碼位于活動子主題(或活動主題)的 function.php 文件中.經(jīng)測試有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
沒有代碼 (正常的woocommerce行為):
使用代碼 (隱藏屬性下拉列表和價格):
它將為您提供與簡單產(chǎn)品相同的用戶界面
It will give you the same UI than simple products
這篇關(guān)于隱藏在 Woocommerce 中默認(rèn)選擇具有獨特變體的可變產(chǎn)品下拉菜單的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!