Repository › Forums › Support › WooCommerce Extended Coupon Features PRO › add a product on the cart to get discount on other category › Reply To: add a product on the cart to get discount on other category
October 23, 2018 at 7:19 am
#3996
Soft79
Keymaster
1) Create a coupon with these settings:
Discount type: Percent discount
Discount amount: 10
Products: Bag
Categories: Shoes
2) Paste the following snippet in your child theme’s functions.php and replace COUPON_CODE and 123 with the actual coupon code and id of the Shoes-category (you can find the category id by looking at the url of the category’s edit page; look for something like &tag_ID=123 ):
add_filter( 'woocommerce_coupon_get_discount_amount', function( $discount, $discounting_amount, $cart_item, $single, $coupon ) {
$COUPON_CODE = 'COUPON_CODE'; // replace with the coupon code
$DISCOUNTED_CATEGORY = 123; // replace with the category that is discounted
if ( 0 == strcasecmp( $coupon->code, $COUPON_CODE ) ) {
$cat_ids = $cart_item['data']->get_category_ids();
if ( ! in_array( $DISCOUNTED_CATEGORY, $cat_ids ) ) {
$discount = 0;
}
}
return $discount;
}, 10, 5 );
I haven’t tested the snippet, so let me know if it doesn’t work.