Repository Forums Support WooCommerce Extended Coupon Features PRO add a product on the cart to get discount on other category

Topic Resolution: Resolved
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #3994
    marianaferreira
    Participant

    ​Hello!
    I wanted to make a coupon discount that gives 10% off on the category Shoes when the client buy our new Bag together, but i don’t want the discount to apply on the bag, just the shoes, is that possible? I tried to make it but i failed, the coupom apply to both of them, can you help me with it? Thanks!

    #3995
    marianaferreira
    Participant
    #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.

    #3999
    marianaferreira
    Participant

    Hello,

    Thanks for the quick reply!
    I replaced what you asked on the code but it haven’t worked yet.
    That’s no discount applyed anymore on the shopping cart

    Category Shoes: https://imgur.com/a/j61WGKr
    Snipped: https://imgur.com/a/QpzD0np
    Shopping cart with snipped active: https://imgur.com/a/guH1CRK

    Shopping cart with snipped innactive: https://imgur.com/a/zByKH1z

    Can you help me to fix it? Thanks in advance!

    #4000
    Soft79
    Keymaster

    Probably because it’s a variable product. Try this:

    
    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 ) ) {
        $_product = $cart_item['data'];
        if ( $_product->get_parent_id() ) {
          $_product = wc_get_product( $_product->get_parent_id() );
        }
        $cat_ids = $_product->get_category_ids();
        if ( ! in_array( $DISCOUNTED_CATEGORY, $cat_ids ) ) {
          $discount = 0;
        }
      }
      return $discount;
    }, 10, 5 );
    
    #4001
    marianaferreira
    Participant

    It worked now! Thanks a lot for the excelent support 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.