在WooCommerce可变产品下拉列表中隐藏特定的产品属性术语



我想用所有值隐藏一个属性。请查看链接。https://csepromos.nl/product/oregon-400-ml-drinkfles-met-karabijnhaak/

我想在产品页面上隐藏以下属性和以下值:

过滤器kleur:Grijs Groen Oranje Paars Rood Wit Zwart

这可以用代码完成吗?

我找到了下面的代码,这有点帮助,但我没有下拉菜单,以及如何一次隐藏所有值?

https://stackoverflow.com/a/54987217/13407118

谢谢!

您可以使用以下基于"Color">产品属性的示例,其中2个值将从变量产品的属性下拉列表中删除。

您需要设置产品属性分类法,该分类法始终以pa_开头,然后是产品属性slug。因此,对于"颜色">产品属性,分类法为pa_color

然后,你将在一个数组中设置你想要的产品属性术语名称(你想从下拉列表中隐藏的名称。

add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_dropdown_variation_args', 10, 1 );
function filter_dropdown_variation_args( $args ) {
// HERE set your product attribute taxonomy (always start with "pa_" + the slug
$taxonomy = 'pa_color';
// HERE set the product attribute terms names that you want to hide
$targeted_terms_names = array( "Blue", "Red" );

// Convert term names to term slugs
$terms_slugs = array_filter( array_map( 'sanitize_title', $targeted_terms_names ) );
// Targeting a product attribute only
if( $args['attribute'] === $taxonomy ) {
// Loop through the product attribute option values
foreach( $args['options'] as $key => $option ){
if( in_array( $option , $terms_slugs ) ) {
unset($args['options'][$key]);
}
}  
}
return $args;
}

代码位于活动子主题(或活动主题(的functions.php文件中。测试并工作。

请注意,您不能从下拉列表中隐藏具有所有术语值的产品属性,因为客户将无法选择任何产品变体。

最新更新