Skip to content
Advertisement

WooCommerce: Double discount on sale products with coupon

I want to double the discount for products on sale with a coupon code.

For example: The product is on sale with a 10% discount. If I add the coupon code doublediscount I want to double that discount to 20%.

The coupon discount should have limit of 15%. So if a product is on sale with a 30% discount, the max added discount with the coupon code should be 15%. Resulting in a 45% discount on the regular price (sale + extra discount).

My code so far is this:

JavaScript

I loop through all cart items and check if they are on sale and if the discount is below 15%. If that’s the case, I want to change the discount for these cart items.

If the cart item has a discount above 15% I don’t want to do anything. So the coupon code doublediscount would apply 15% to them.

I just don’t know how to add/change the discount of a cart item.

Advertisement

Answer

You can use the woocommerce_coupon_get_discount_amount hook instead in combination with the following coupon settings:

  • Set correctly your coupon code: doublediscount
  • Discount type: Percentage
  • Amount: 15

Steps applied in this answer:

  • Only if the specific coupon code matches and the product is on sale
  • If a product is not on sale, no discount will be applied (by the else condition equal to 0. However, if this doesn’t apply, you can simply remove the else condition)
  • Current percentage discount of the on sale product is calculated. If this is less than the maximum added discount (15), then the discount is doubled
  • If this is more, the maximum discount added (15) will be applied automatically

So you get:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement