# How can I apply a custom discount based on line item's value in Shopify?

Visitor
1 0 0

I am building out a rather complicated discount/proration calculator in my store. For some background knowledge: I have a physical product that customers purchase and with that, they need to purchase a subscription plan. The subscription plan for the first month has some tricky proration. For example, a customer checking out on March 27th would have to pay a prorated amount for the remainder of March (1 -(27/31 = .87096..)) = .12903...) March subscription cost * .12903 + Whole cost of April. So basically 1 month + .13 month for the remainder of March.

I already have all of this functionality worked out. But there's one piece I'm stuck on:

I used this tutorial to create a line item property in my product-form.liquid (on Motion theme). The custom JS code I wrote here just finds the day in the current month and divides it by the total amount of days in the month, For example: if a customer is checking out on March 27th, the code just divides 27 by 31 (days in March). Then I attach that number value to a line item property. This is all working perfectly fine. I have some functionality in my store already that adds 2x the subscription product they purchase to the cart and adds the physical product to the cart as well.

Heres a simplified example:

``````<p class="line-item-property__field">
<input id="proration" type="hidden" name="properties[_proration]">
<script>
var d = new Date();
// Get today's date
var day = d.getDate();
var month = d.getMonth() + 1; // The months are 0-based
var year = d.getFullYear();
var daysInMonth = getDaysInMonth(day, year);
var prorate = day / daysInMonth;
document.getElementById("proration").value = prorate;
</script>
</p>``````

But I now need to use Shopify scripts, to discount one of those subscription items in the cart, based on the percentage calculation I made and attached previously to the "proration" field.

I have tried a few things to no avail. Hoping someone in here knows what to do!

Replies 2 (2)
Shopify Partner
72 18 33

Looks like you've done most of the heavy lifting here. Now, we just have to do the following during checkout:

• look for a line item property "_proration"
• check that the property has a value greater than 0.00
• create a variable called "prorated_discount" with that value
• update the line item's price to be the new discounted amount

Here is the script for that functionality:

``````cart = Input.cart
cart.line_items.each do |line_item|
if line_item.properties.has_key?("_prorated") && (line_item.properties["_prorated"].to_f > 0.00) && (line_item.properties["_prorated"].to_f < 1.00)
prorated_discount = line_item.properties["_prorated"].to_f
line_item.change_line_price(line_item.line_price * prorated_discount, message: 'Prorated charge for the rest of this month')
end
end
Output.cart = cart``````

And here it is working in the checkout preview:

NOTE 1: The extra conditional on line 4 (this part - " && (line_item.properties["_prorated"].to_f < 1.00)"), is only really needed if you are adding the "_prorated" property to ALL products. It will probably work without it.

NOTE 2: you will need to save and preview the script from the Script Editor app by clicking "preview on the Online Store". This is because you cannot easily add line item properties in the Script Editor cart preview