What I want:
- I have created a script in the Script Editor that makes your first
line item in a cart free. I want this script to execute once per customer.
My question:
- What is the syntax to limit a script to execute once per customer
through the use of the Script Editor app?
Business use case:
- Without adding this functionality to my script, a customer could
checkout with 1 item free… but then they could begin shopping again
and checkout with a new cart with the first line item free again… that
means they could get 10 free items from 10 different checkouts.
Thanks for your time and thoughts!
P.S. Script is in Ruby. Also, Setting up a discount code won’t work for my use case.
UPDATE
Thank you for your responses. I will be attempting these suggested updates this week and will post my results soon.
UPDATE 2
So the below suggestions and marked answer were all spot on except for one detail. The script editor keeps on running into some type of error even though the described syntax below is correct. Another stackoverflow answer suggested to use ‘&’.
So I ended up doing something like this:
customer = Input.cart.customer
if customer&.tags&.include?("gift_received")
...
end
This works for me. Thanks again for all your answers!
3
Answers
Um. You can always just assign a little tag to the customer. For example, if they received this free item, give them a tag on their first order. Now, if they try and fool you nine more times, check for that tag. If they have it, remove the item from checkout. Boom. problem solved.
Any kind of solution could only work if Accounts are required
otherwise you can’t track the history of orders.
There are two possible solutions
Input.cart.customer.tags.include?('gift_received')
and don’t apply the discount.Input.cart.customer.orders_count > 0
and that’s it.The best way is to flag customers using tags. You can use Shopify flows to add tags to orders containing free gifts to mark customers as the customers who received already free gifts. This will be better than checking
Input.cart.customer.orders_count > 0
because it will give you more visibility in the orders & customers panel.The approach mentioned by @Fabio Filippi is the best however it needs to be secured:
If you want to avoid fraud I would consider using both (note that in this case we are checking if customer already bought something in the past)
Input.cart.customer.orders_count > 1
andInput.cart.customer.tags.include?('gift_received')
ORInput.cart.customer.tags.include?('gift_received')
andInput.cart.customer.total_spent > Money.new(cents: 1000)
. This way you can have "eligible for gift" kind of logic. There are many ways to approach it.