Ruby script to calculate discount using Shopify Scripts

1k Views Asked by At

I am using a script to calculate an addtional 5% off when the cart total is greater than $1000, but the variant price is already discounted by 25% and has a was/now pricing setup that is $29.95 was $39.95.

The problem is - i need for the script to discount the orginal item value of $39.95, not the discounted rate which is the new retail price.

Here is the script - i have tried to add 25% back to the line item price without success and there is no line item method that Shopify provides to use the orginal cart line item price in the dev docs - https://help.shopify.com/en/manual/checkout-settings/script-editor/shopify-scripts#line-item-methods

# Tiered Discounts by Spend Threshold 5% off when you spend $1000 or more
min_discount_order_amount_extra = Money.new(cents:100) * 1000 #number of dollars needed in cart to get the discount

DISCOUNT_PERCENT = 25

# calculate the discount to be applied
percent = Decimal.new(DISCOUNT_PERCENT/100) + 1

total = Input.cart.subtotal_price_was * percent

discount = if total > min_discount_order_amount_extra
              0.05 #discount percentage in decimal form
            else
              0
            end
message = "Another 5% saved"

# 30% off products excluding commercial items
Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  next if product.gift_card?
  next if line_item.variant.product.tags.any?{|x| ['commercial'].include?(x)};
  line_item.change_line_price(line_item.line_price * (1-discount), message: message) unless discount == 0
end

Output.cart = Input.cart

0

There are 0 best solutions below