I’m using a custom code to prevent products from all coupons.
For that, every product has an extra checkbox in the backend.
The code is mostly based on that answer: https://stackoverflow.com/a/47603782/1788961
You can see the backend checkbox here:
Everything works fine so far.
But after we’ve changed our bundles to have the bundled products be priced individually, the checkbox doesn’t work anymore.
I guess that the code doesn’t apply on the product inside a bundle.
This is the code to make coupons invalid at product level and set the product discount amount to zero:
// Make coupons invalid at product level
add_filter('woocommerce_coupon_is_valid_for_product', 'set_coupon_validity_for_excluded_products', 12, 4);
function set_coupon_validity_for_excluded_products($valid, $product, $coupon, $values ){
if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $valid;
$disabled_products = get_option( '_products_disabled_for_coupons' );
if( in_array( $product->get_id(), $disabled_products ) )
$valid = false;
return $valid;
}
// Set the product discount amount to zero
add_filter( 'woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5 );
function zero_discount_for_excluded_products($discount, $discounting_amount, $cart_item, $single, $coupon ){
if( ! count(get_option( '_products_disabled_for_coupons' )) > 0 ) return $discount;
$disabled_products = get_option( '_products_disabled_for_coupons' );
if( in_array( $cart_item['product_id'], $disabled_products ) )
$discount = 0;
return $discount;
}
Is there a way to check the product type of a cart item and disable the coupon on bundled products?
I know how to check for the product type and I tried a few things. But nothing worked so far.
I tried some things and the code from Howard E in the answer below. Unfortunately nothing works for now.
But I realised something.
If the bundle has a regular price and a sale price, everything works as it should. But if the bundle product is piced individually (as seen in the screen below), the code doesn’t work anymore.
I guess I have to chech if an item is part of a parent bundle item?
Is that possible in the cart?
2
Answers
I found a solution!
I changed the if statements to the following code:
and:
By adding
wc_pb_is_bundled_cart_item($cart_item) === true
I could check if the cart item is part of a bundle and exclude the item from any coupon.I’m unable to test this, but it would seem that the following should work.