Repository › Forums › Support › WooCommerce Extended Coupon Features PRO › Add 2 different products and get 5% discount on specific product
- This topic has 5 replies, 2 voices, and was last updated 1 year, 8 months ago by Loes Arentsen.
-
AuthorPosts
-
June 3, 2021 at 4:50 pm #17705Loes ArentsenParticipant
Hi there,
For a client I’m looking for a solution.
If somebody buys product A (variable prodyct with variable prices) and adds a product out of category B, the customer should get 5% discount on product A. So the discount depends on the chosen variation.Hope you can help me with this. Thanks!
LoesJune 4, 2021 at 11:28 am #17709Soft79KeymasterYou need to apply a snippet similar to https://www.soft79.nl/forums/topic/buy-product-a-get-30-off-each-product-b/#post-3029 . Are you comfortable with pasting php snippets? I can alter the snippet so it will accept category B instead of only a single product of type B.
June 4, 2021 at 1:03 pm #17710Loes ArentsenParticipantYes pasting php is not a problem at all!
Thanks so much!June 7, 2021 at 7:34 pm #17715Soft79KeymasterThis snippet should work.
Create a 5% discount coupon that only applies to product A.
Paste the snippet in your child theme’s functions.php and replace the value after $required_cat with category B’s id and replace the value after $coupon_code with the coupon code. The snippet will invalidate the coupon if there’s no item from category B in the cart.
add_filter( 'woocommerce_coupon_is_valid', function( $valid, $coupon, $wc_discounts ) { $required_cat = 6666; $coupon_code = 'THE_COUPON_CODE'; if ( strcasecmp( $coupon->get_code(), $coupon_code ) !== 0 ) { return $valid; } if ( ! class_exists( 'WJECF_WC_Discounts' ) ) { return $valid; } if ( ! $valid ) { return $valid; } if ( is_null( $wc_discounts ) ) { $wc_discounts = new WJECF_WC_Discounts( WC()->cart ); } //Get array of all cart product and variation ids $product_cats = array(); foreach ( $wc_discounts->get_items() as $item_key => $item ) { if ( ! $item->product ) { continue; } $product_id = $item->product->get_id(); if ( 'product_variation' == get_post_type( $product_id ) ) { $product_id = $item->product->get_parent_id(); } $product_cats = array_merge( $product_cats, wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ) ); } if ( ! in_array( $required_cat, $product_cats ) ) { $valid = false; } return $valid; }, 10, 3 );
Note: untested.
October 28, 2021 at 6:38 am #18161Loes ArentsenParticipantHi there,
In addition to this: is it possible to do this with just categories as well?
So when somemebody buys a product from category A and category B, he gets 5% discount on the product of category B.Hope to hear from you 🙂
January 13, 2022 at 12:06 pm #18462Loes ArentsenParticipant@soft79 any help on my last message?
Thanks! -
AuthorPosts
- You must be logged in to reply to this topic.