skip to Main Content

I have a client who wants to add a coupon for WordPress/WooCommerce checkout. Normally I would just go to WooCommerce > Coupons and create the coupon.

However, they want one coupon code to give a product discount to TWO separate products. Products in this case are admission tickets.

  • coupon code name is = “COUPON1”
  • “PRODUCT A” is $15.95 on weekdays (product ID = 2846)
  • “PRODUCT A” is $17.95 on weekends (product ID = 2848)
  • “PRODUCT B” is $10.95 on weekdays (product ID = 2847)
  • “PRODUCT B” is $12.95 on weekends (product ID = 2849)
  • PRODUCT A discount is $11.95 for both weekday and weekend tickets
  • PRODUCT B discount is $8.95 for both weekday and weekend tickets

Workflow:

  1. User adds 1 ticket for PRODUCT A to cart
  2. User adds 1 ticket for PRODUCT B to cart
  3. User enters coupon COUPON1 to discount field
  4. WordPress hook notices these products in cart by their ID and notices that the coupon COUPON1 has been applied
  5. WordPress creates the discounted prices for PRODUCTS A and B
  6. Subtotal now shows $11.95 + $8.95 = $20.90

I can create one coupon code for the $15.95 weekend and weekday tickets but it can’t be applied to the second product (PRODUCT B).

The discount to $11.95 should be applied IF the coupon is applied and IF any of the product IDs 2846 or 2848 are in the cart.

The discount to $8.95 should be applied IF the coupon is applied and IF any of the product IDs 2847 or 2849 are in the cart.

I figured there is probably a webhook that can be written that will do this for me.

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    Here is what I did, in case anybody else wants to do this in the future:

    1. create coupon in WooCommerce > Coupons, assigned it to the 4 products tied to this one coupon but gave it a discount of $0. This coupon will be triggered in the next step.
    2. In WC Pricing & Discounts plugin > Cart Discounts tab, create one rule for each of the products with its Fixed Discount per Item, assign that rule to the individual product (ie product ID = 2846 is $4 off) and give it the condition that COUPONS APPLIED = COUPON1
    3. do the same for each of the product IDs
    4. in the cart it will show the row with the coupon name when applied, so I just hid that row using CSS display:none for class .coupon-COUPON1
    5. Make sure that APPLY ALL APPLICABLE RULES is enabled in Cart Discounts tab
    6. Now each of the rows rules show when those products are in the cart and to stylize the rows better I added a break tag in the rule title (ie Coupon: COUPON1 [br] Discount for Product 2846) and then with CSS added line-height:1.75em to class .cart-discount

  2. I don’t think this can be done with one coupon code. At first I thought you need one coupon code for weekdays and weekends to change automatically, but having to different discounts for two different products is impossible.

    What you can do, is adding the a second coupon dynamically on the fly. Have a “secret” coupon on the back-end that will control only over the Product B (using the coupon limitations – choose Product B only to be affected).

    Then add and adjust the coupon dynamically each time the Product B is in cart:

    define('PRODUCT_B_ID', 2487);
    add_filter( 'woocommerce_add_to_cart_handler', 'apply_secret_coupon', 50, 2);
    
    function apply_secret_coupon($product_type, $product){
        if ($product->get_id() == PRODUCT_B_ID){
            $calculated_discount_amount = 15.22;
            $secret_coupon     = new WC_Coupon('MySecretCoupon');
            $secret_coupon->set_amount($calculated_discount_amount);
            $secret_coupon->save();
            WC()->cart->apply_coupon('MySecretCoupon');
        }
     return $product_type;
    }
    

    I noticed you are using few product IDs. This code can give you a solution if you modify it for your needs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search