Exclude variations with 2 specific attribute terms from coupon usage in Woocommerce
Solution 1:
This can be done using woocommerce_coupon_is_valid
filter hook this way:
add_filter( 'woocommerce_coupon_is_valid', 'check_if_coupons_are_valid', 10, 3 );
function check_if_coupons_are_valid( $is_valid, $coupon, $discount ){
// YOUR ATTRIBUTE SETTINGS BELOW:
$taxonomy = 'pa_style';
$term_slugs = array('swirly', 'circle');
// Loop through cart items and check for backordered items
foreach ( WC()->cart->get_cart() as $cart_item ) {
foreach( $cart_item['variation'] as $attribute => $term_slug ) {
if( $attribute === 'attribute_'.$taxonomy && in_array( $term_slug, $term_slugs ) ) {
$is_valid = false; // attribute found, coupons are not valid
break; // Stop and exit from the loop
}
}
}
return $is_valid;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.