从WooCommerce优惠券中排除具有特定属性条款的产品



我想从所有WooCommerce优惠券中排除具有特定属性的产品(例如attribute_pa_brand=>mybrand(。

我遵循了这个答案。在Wooccommerce中,从优惠券使用中排除具有2个特定属性项的变体,但它似乎只适用于可变产品类型,而我只需要它用于

简单产品如果有任何答复,我将不胜感激。

问题中的链接代码适用于产品变体类型。现在,为了使其适用于简单产品和可变产品(不针对特定的变体属性术语(,您将使用以下内容:

add_filter( 'woocommerce_coupon_is_valid', 'custom_coupon_validity', 10, 3 );
function custom_coupon_validity( $is_valid, $coupon, $discount ){
// YOUR ATTRIBUTE SETTINGS BELOW:
$taxonomy   = 'pa_brand'; // Set the taxonomy (start always with "pa_" + the slug)
$term_names = array('Apple', 'Sony'); // Set your term NAMES to be excluded
$term_ids = array(); // Initializing
// Convert term names to term Ids
foreach ( $term_names as $term_name ) {
// Set each term id in the array
$term_ids[] = get_term_by( 'name', $term_name, $taxonomy )->term_id;
}
// Loop through cart items and check for backordered items
foreach ( WC()->cart->get_cart() as $cart_item ) {
$product = $cart_item['variation_id'] > 0 ? wc_get_product($cart_item['product_id']) : $cart_item['data'];
// Loop through product attributes
foreach( $product->get_attributes() as $attribute => $values ) {
if( $attribute === $taxonomy && array_intersect( $values->get_options(), $term_ids ) ) {
$is_valid = false; // attribute found, coupons are not valid
break; // Stop and exit from the loop
}
}
}
return $is_valid;
}

代码位于活动子主题(或活动主题(的functions.php文件中。经过测试,适用于WooCommerce上一个版本的简单和可变产品。

最新更新