隐藏在 Woocommerce 中默认选择具有独特变体的可变产品下拉菜单
我有一个独特的问题.我正在使用他们无法支持请求的插件.我需要将变体拆分为单独的项目,但是如果我复制和粘贴并将它们变成一个简单的产品,那么我无法同步产品的计数以跟踪库存.作为一种解决方法,我需要能够禁用不需要的变体,只保留我确实需要的变体.但这是我遇到麻烦的地方.如果我启用了一个变体,那么我不想显示下拉列表,而是希望它在 UI 上看起来像一个简单的产品.我尝试了一切,但无法让它工作.
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 );
没有成功,因为即使计数为 0、价格为 0 且项目已禁用,它们仍可见且未隐藏.
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.
如何显示没有下拉菜单的产品页面?更进一步;我删除了所有变体,但我需要保留的变体除外.由于同步插件,我需要将其保持在变化模式.如何在不显示任何下拉列表的情况下显示它?
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?
示例逻辑:
如果产品类型是变体并且启用计数 == 1 则特殊 ui 显示,否则正常.
If product type is a variation and enabled count == 1 then special ui display, else normal.
谢谢.
推荐答案
重要提示:代码仅适用当唯一变体被选择为默认表单值时:
IMPORTANT: The code only works when the unique variation is selected as default form value:
以下代码将隐藏仅启用一个变体并默认选择的变量产品、属性下拉列表和所选变体价格:
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 文件中.经测试有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
没有代码 (正常的woocommerce行为):
使用代码 (隐藏属性下拉列表和价格):
它将为您提供与简单产品相同的用户界面
It will give you the same UI than simple products
相关文章