• Resolved toolbeltscharlie

    (@toolbeltscharlie)


    Hi. I created a “Buy Item A and Get Item B 1/2 Off” coupon. The coupon should discount a fixed amount (1/2 of Item B price) when BOTH items A and B are in the cart. So I input both A and B in the “Products” field under “Restrictions” tab. But the coupon only validates against one OR the other, not BOTH! Is it really supposed to be an OR clause in play here or should it be AND, which is what the documentation appears to indicate?!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @toolbeltscharlie

    I created a “Buy Item A and Get Item B 1/2 Off” coupon. The coupon should discount a fixed amount (1/2 of Item B price) when BOTH items A and B are in the cart. So I input both A and B in the “Products” field under “Restrictions” tab. But the coupon only validates against one OR the other, not BOTH! Is it really supposed to be an OR clause in play here or should it be AND, which is what the documentation appears to indicate?

    To be able to correctly answer your question I need to know whether you are using a particular plugin for the coupons, or the inbuilt coupon feature in WooCommerce core?

    You can also share a copy of your site’s System Status so we can check which plugin is in use. You can find it via WooCommerce > Status. Select “Get system report” and then “Copy for support”. Once you’ve done that, paste it here in your response.

    We’ll look forward to your response and will be happy to assist further.

    Thread Starter toolbeltscharlie

    (@toolbeltscharlie)

    Thank you! Yes, I am using the core inbuilt coupon feature, no additional plugins involved here.

    From your docs concerning the product restriction setting: “Products that the coupon will be applied to, or that need to be in the cart in order for the fixed or percentage discount to be applied.”

    I want the coupon to validate ONLY if Items A AND B are BOTH present in the cart. Not EITHER/OR! This seems like the logical way it should work but I wanted to check.

    Thank you!

    Thread Starter toolbeltscharlie

    (@toolbeltscharlie)

    Please close this ticket. WC_Discounts::validate_coupon_product_ids confirms it’s an ANY not ALL.

    I’ll share code I used to resolve my issue in case it’s helpful to anyone.

    I was able to utilize the hook okay but I ended up with (slightly tweaked) duplicate code.

    add_filter( 'woocommerce_coupon_is_valid', function ($valid, \WC_Coupon $coupon) {
        if ( count( $coupon->get_product_ids() ) > 0 ) {
            $valid_products = [];
    
            $cart_items = ( new \WC_Discounts(WC()->cart) )->get_items_to_validate();
            $restricting_product_ids = $coupon->get_product_ids();
            foreach ( $cart_items as $item ) {
                if ( $item->product &&
                    (
                        in_array( $item->product->get_id(), $restricting_product_ids, true ) ||
                        in_array( $item->product->get_parent_id(), $restricting_product_ids, true )
                    )
                ) {
                    //count w/ de-duper
                    $valid_products[$item->product->get_parent_id()] = true;
                }
            }
    
            $valid = count($valid_products) === count($restricting_product_ids);
    
            return $valid;
        }
    
        return $valid;
    }, 10, 2);

    Hello,
    Glad to hear that you figured this out.

    Thanks for sending your workaround, if you would like, we’d love it if you could share your experience with the community by leaving a review: https://wordpress.org/support/plugin/woocommerce/reviews/

    Best.

    There’s also Bundle Style Coupons if I can get the author to release version .3 … or in the meantime my fork has the necessary updates

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Validate coupon against multiple products in cart’ is closed to new replies.