Tourist
6 0 1

## Discount Based on Cart Total AFTER Coupon is Applied

We are running a script to give a specific product for free based on the total amount in the cart. But it calculates the cart total before a coupon code is added. Is there a way to recheck if the total threshold has been met after a coupon has been added?

Our current script

```FREEBIE_PRODUCT_ID = 1111111111
CART_TOTAL_FOR_DISCOUNT_APPLIED = Money.new(cents: 100) * 125
DISCOUNT_MESSAGE = "FREE Item Name When You Spend Over 125"

freebie_in_cart = false
cart_price_exceeds_discounted_freebie_amount = false
cost_of_freebie = Money.zero

# Test if the freebie is in the cart, also get its cost if it is so we can deduct from the cart total
Input.cart.line_items.select do |line_item|
product = line_item.variant.product
if product.id == FREEBIE_PRODUCT_ID
freebie_in_cart = true
cost_of_freebie = line_item.line_price
end
end

# If the freebie exists in the cart, check the subtotal of the other items to see if the freebie should be discounted
if freebie_in_cart
cart_subtotal_minus_freebie_cost = Input.cart.subtotal_price - cost_of_freebie
if cart_subtotal_minus_freebie_cost >= CART_TOTAL_FOR_DISCOUNT_APPLIED
cart_price_exceeds_discounted_freebie_amount = true
end
end

# Only true if the freebie is in the cart
was_discount_applied = false
if cart_price_exceeds_discounted_freebie_amount
Input.cart.line_items.each do |item|
if item.variant.product.id == FREEBIE_PRODUCT_ID && was_discount_applied == false
if item.quantity > 1
new_line_item = item.split(take: 1)
new_line_item.change_line_price(Money.zero, message: DISCOUNT_MESSAGE)
Input.cart.line_items << new_line_item
next
else
item.change_line_price(Money.zero, message: DISCOUNT_MESSAGE)
end
end
end
end

Output.cart = Input.cart```
Replies 3 (3)
Shopify Partner
1540 43 332

## Re: Discount Based on Cart Total AFTER Coupon is Applied

The .subtotal_price method on Cart object "Returns the subtotal price of the cart after line item discounts are applied but before discount codes are applied."

So in Scripts you do not know what is applied via a coupon.

You can hardcode the discount coupons values and make the math but this will be really maintainable can can be error prone

New Member
23 0 0

## Re: Discount Based on Cart Total AFTER Coupon is Applied

Hey,

I know one app that can help you with this. Kart Discount App (https://apps.shopify.com/discount-coupon-field-in-cart-page) can help you with the customization that you need. They did the same for one of their clients. Download the app & contact their support team. They are really fast!

Shopify Partner
13 0 1

## Re: Discount Based on Cart Total AFTER Coupon is Applied

Hey!

Did someone found a way the perfect way to get the discounted amount by discount code in cart?

I tried this but it does not work for BUY X GET Y discounts code :

``````class Discounts
def self.discount_amount(cart)
if cart.discount_code.class == CartDiscount::Percentage
(cart.discount_code.percentage / 100) * cart.subtotal_price
elsif cart.discount_code.class == CartDiscount::FixedAmount
cart.discount_code.amount
else
0
end
end
end

discount_amount = Discounts.discount_amount``````
Top Contributors
Community Browser