在 Woocommerce 产品变体中隐藏特定属性值的“添加到购物车”按钮
Hide Add to Cart button in Woocommerce product variations for a specific attribute value
在 Woocommerce 中,我试图隐藏“添加到购物车”按钮以获取具有特定所选属性值的变体。每个变体都有两个属性(pa_color
和 pa_size
)
例如,对于可变产品,我们有以下选项:
1) Red - XL
2) Red - XXL
3) Blue - M
4) Blue - XL
我想隐藏 XL
的添加到购物车按钮,这样用户就无法将具有 XL 的选项添加到购物车(本例中为 1 和 4)
P.S:
我们不想禁用变体,因此可以通过选择此选项来显示变体图像,因此停用变体或删除价格以及.. 不是我们的解决方案。
这是使添加到购物车按钮无效的方法,产品属性"pa_size"带有"XL" 值:
add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
function conditional_variation_is_purchasable( $purchasable, $product ) {
## ---- Your settings ---- ##
$taxonomy = 'pa_size';
$term_name = 'XL';
## ---- The active code ---- ##
$found = false;
// Loop through all product attributes in the variation
foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
$attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
$term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
// Searching for attribute 'pa_size' with value 'XL'
if($attribute_taxonomy == $taxonomy && $term->name == $term_name ){
$found = true;
break;
}
}
if( $found )
$purchasable = false;
return $purchasable;
}
代码进入您的活动子主题(或主题)的 function.php 文件。已测试并有效。
将您的 slug 文本用于 term_name
add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
function conditional_variation_is_purchasable( $purchasable, $product ) {
## ---- Your settings ---- ##
$taxonomy = 'pa_size';
$term_name = 'XL';
## ---- The active code ---- ##
$found = false;
// Loop through all product attributes in the variation
foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
$attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
$term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
// Searching for attribute 'pa_size' with value 'XL'
if($attribute_taxonomy == $taxonomy && $term->slug == $term_name ){
$found = true;
break;
}
}
if( $found )
$purchasable = false;
return $purchasable;
}
在 Woocommerce 中,我试图隐藏“添加到购物车”按钮以获取具有特定所选属性值的变体。每个变体都有两个属性(pa_color
和 pa_size
)
例如,对于可变产品,我们有以下选项:
1) Red - XL
2) Red - XXL
3) Blue - M
4) Blue - XL
我想隐藏 XL
的添加到购物车按钮,这样用户就无法将具有 XL 的选项添加到购物车(本例中为 1 和 4)
P.S: 我们不想禁用变体,因此可以通过选择此选项来显示变体图像,因此停用变体或删除价格以及.. 不是我们的解决方案。
这是使添加到购物车按钮无效的方法,产品属性"pa_size"带有"XL" 值:
add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
function conditional_variation_is_purchasable( $purchasable, $product ) {
## ---- Your settings ---- ##
$taxonomy = 'pa_size';
$term_name = 'XL';
## ---- The active code ---- ##
$found = false;
// Loop through all product attributes in the variation
foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
$attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
$term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
// Searching for attribute 'pa_size' with value 'XL'
if($attribute_taxonomy == $taxonomy && $term->name == $term_name ){
$found = true;
break;
}
}
if( $found )
$purchasable = false;
return $purchasable;
}
代码进入您的活动子主题(或主题)的 function.php 文件。已测试并有效。
将您的 slug 文本用于 term_name
add_filter( 'woocommerce_variation_is_purchasable', 'conditional_variation_is_purchasable', 20, 2 );
function conditional_variation_is_purchasable( $purchasable, $product ) {
## ---- Your settings ---- ##
$taxonomy = 'pa_size';
$term_name = 'XL';
## ---- The active code ---- ##
$found = false;
// Loop through all product attributes in the variation
foreach ( $product->get_variation_attributes() as $variation_attribute => $term_slug ){
$attribute_taxonomy = str_replace('attribute_', '', $variation_attribute); // The taxonomy
$term = get_term_by( 'slug', $term_slug, $taxonomy ); // The WP_Term object
// Searching for attribute 'pa_size' with value 'XL'
if($attribute_taxonomy == $taxonomy && $term->slug == $term_name ){
$found = true;
break;
}
}
if( $found )
$purchasable = false;
return $purchasable;
}