Home > OS >  WooCommerce: Exclude bundles from all coupons
WooCommerce: Exclude bundles from all coupons

Time:11-20

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: custom checkbox in product general settings metabox

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?

enter image description here

CodePudding user response:

I'm unable to test this, but it would seem that the following should work.

// 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 ( 'bundle' === $product->get_type() ) {
        $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 ) {

    $product = wc_get_product( $cart_item['product_id'] );

    if ( 'bundle' === $product->get_type() ) {
        $discount = 0;
    }

    return $discount;
}

CodePudding user response:

I found a solution!

I changed the if statements to the following code:

if( in_array( $product->get_id(), $disabled_products ) or wc_pb_is_bundled_cart_item($cart_item) === true )

and:

if( in_array( $cart_item['product_id'], $disabled_products ) or wc_pb_is_bundled_cart_item($cart_item) === true )

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.

  • Related