Make script run only once

Nikolas_Alexand
New Member
28 0 0

Hi, I wrote this script to discount $69 off any product with a black friday tag. I want the script to only give the discount ONCE. Right now, if you add a black friday product, it'll give you the discount, and then give you the discount again if you add another BF product.

 

min_discount_order_amount = Money.new(cents:100) * 219 #minimum amount needed to have in cart to get discount
total = Input.cart.subtotal_price_was
discount.total_amount = if total > min_discount_order_amount
              6900 #discount amount you are offering in cents
            else
              0
            end
message = "Discount applied" #discount message shown to customer

Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  next if product.gift_card?
  next unless product.tags.include?('BlackFriday')
  line_item.change_line_price(line_item.line_price - Money.new(cents: discount), message: message) unless discount == 0
  end

Output.cart = Input.cart

 

0 Likes
Nikolas_Alexand
New Member
28 0 0

Basically, I want the script to discount for only one item.

0 Likes
Nikolas_Alexand
New Member
28 0 0

Got it working. Here's the script for anyone who wants this in the future. 

 

~~~#if product.tags.detect{ |e| product.tags.count(e) > 1 }
# THIS SCRIPT RUNS EVERY TIME A CUSTOMER ADDS AN ITEM TO THEIR CART 
# count the number of items in the cart with the black friday tag
bf_tag_ct = 0
Input.cart.line_items.each do |line_item|
  next if line_item.variant.product.tags.include?('BlackFriday')
    bf_tag_ct = bf_tag_ct + 1
end

#if bf_tag_ct == 1
  min_discount_order_amount = Money.new(cents:100) * 219 #minimum amount needed to have in cart to get discount
  total = Input.cart.subtotal_price_was
  discount = if total > min_discount_order_amount
                  6900 #discount amount you are offering in cents
                else
                  0
                end
  message = "Discount applied" #discount message shown to customer
  first_item_flag = 0 
  Input.cart.line_items.each do |line_item|
      product = line_item.variant.product
      next if product.gift_card?
      next unless product.tags.include?('BlackFriday') && first_item_flag==0
        line_item.change_line_price(line_item.line_price - Money.new(cents: discount), message: message) unless discount == 0
        first_item_flag = 1
  end

#end 

Output.cart = Input.cart

 

0 Likes
Nikolas_Alexand
New Member
28 0 0

.

0 Likes