Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Soft79

    (@josk79)

    Well, my plugin doesn’t do that out-of-the-box, but if you add the following code to your themes’ functions.php, it will!

    Please replace ‘code1’, ‘code2’, ‘etc…’ by the coupon codes that you want the AND-condition to be applied to.

    add_filter('woocommerce_coupon_is_valid', 'my_wc_coupon_validate', 10, 2);
    function my_wc_coupon_validate ($valid_for_cart, $coupon) {
    
    	// Only accept coupon if *ALL* of the coupons' products are in the cart
    
    	//FILL IN THE COUPON CODES BELOW:
    	$coupon_codes = array( 'code1', 'code2', 'etc...' );
    
    	if ($valid_for_cart && in_array( $coupon->code, $coupon_codes ) ) {
    		if ( sizeof( $coupon->product_ids ) > 0 && ! $coupon->is_type( array( 'fixed_product', 'percent_product' ) ) ) {
    			//Get array of all cart item ids
    			$cart_item_ids = array();
    			foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    				$cart_item_ids[] = $cart_item['product_id'];
    				$cart_item_ids[] = $cart_item['variation_id'];
    			}
    
    			//check if every single product is in the cart
    			foreach( $coupon->product_ids as $product_id ) {
    					if ( ! in_array( $product_id, $cart_item_ids ) ) {
    						$valid_for_cart = false;
    						break;
    					}
    			}
    		}
    	}
    	return $valid_for_cart;
    }

    Does this solve your request?

    Thread Starter BillMc

    (@billmc)

    Wow! This should take care of it. I’ll give it a go in the next couple of days and send a few beers/coffee your way. Thanks.

    Plugin Author Soft79

    (@josk79)

    Sorry that it took so long ??

    Please let me know if you have any questions…

    I might add functionality like this to the plugin in the near future.

    Thread Starter BillMc

    (@billmc)

    Works like a charm! Thanks.

    Plugin Author Soft79

    (@josk79)

    Super, thanks for buying me a full lunch!

    Plugin Author Soft79

    (@josk79)

    I just released v2.0.0 of my plugin, this functionality has been added.

    Please let me know if it works for you.

    Thread Starter BillMc

    (@billmc)

    Works great. Thanks again

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘coupons for cart with specific items’ is closed to new replies.