Articles on: Discounts

How to block manual discount codes from being added on top of an automatic script discount

If you want to offer an automatic discount on a checkout upsell, but don't want an additional discount to be added in the 'Discount code' field, this script will help.

Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id") and line_item.properties["__upsell_id"].include? "54ea7069-5dec-4fbe-a50f-aff10f071636"
line_item.change_line_price(line_item.line_price * 0.7, message: "30% OFF")
  if Input.cart.discount_code
   Input.cart.discount_code.reject(message: "Discount already applied")
  end
 end
end
Output.cart = Input.cart


This script will block additional discounts when an upsell with the specified offer ID (54ea7069-5dec-4fbe-a50f-aff10f071636) is added to cart.

if you want to block manual discount codes from applying for multiple upsell offers, you can use the following script structure:

def apply_discount(line_item, upsell_id, discount_factor, discount_message)
  return unless line_item.properties.has_key?("__upsell_id") && line_item.properties["__upsell_id"].include?(upsell_id)
  line_item.change_line_price(line_item.line_price * discount_factor, message: discount_message)
  if Input.cart.discount_code
    Input.cart.discount_code.reject(
      message: "Discount already applied"
    )
  end
end

Input.cart.line_items.each do |line_item|
  apply_discount(line_item, "03e7fe18-fe02-44bc-9517-a0c7a0ea935e", 0.7, "30% OFF")
  apply_discount(line_item, "cf568376-31ee-4c54-8fcf-ab5efa1507c9", 0.7 "30% OFF")
  apply_discount(line_item, "66ca6ee1-de37-4ff3-969b-8f2eb5aa2397", 0.7 "30% OFF")
  apply_discount(line_item, "cb07b47f-f69d-45ad-8941-eeff08491cd6", 0.77, "30% OFF")
  apply_discount(line_item, "612fa215-fb65-4832-9355-154f1529c76d", 0.7, "30% OFF")
  apply_discount(line_item, "f4ca1e53-f489-4e31-8ff3-29c901b37eba", 0.7, "30% OFF")
  apply_discount(line_item, "b78a2517-3513-41c6-a428-dd656d29734e", 0.7 "30% OFF")
  apply_discount(line_item, "54ea7069-5dec-4fbe-a50f-aff10f071636", 0.7, "30% OFF")
end

Output.cart = Input.cart

Updated on: 20/11/2023

Was this article helpful?

Share your feedback

Cancel

Thank you!