Repository Forums Support WooCommerce Extended Coupon Features PRO Matching products AND Products (not OR) Reply To: Matching products AND Products (not OR)

#252
AbrazoTango
Participant

Hi,

The coupon works with no “AND”. In that case, with one of the 2 products, or both, the coupon is applied once.

This became critical for us because we plan to open our shop the 1st of January.

I tried to understand your code and I don’t understand why “variation_id” is used to compare all products in the cart (and seems to rempace the “product_id”?

//Test if ALL products are in the cart (if AND-operator selected in stead of the default OR)
$products_and = get_post_meta( $coupon->id, ‘_wjecf_products_and’, true ) == ‘yes’;
if ( $products_and && sizeof( $coupon->product_ids ) > 1 ) { // We use > 1, because if size == 1, ‘AND’ makes no difference
//Get array of all cart product and variation 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 ) ) {
return false;
}
}
}