Personalized checkout and custom promotions with Shopify Scripts
I am looking to use a LineItem Script to check if a condition is met then if it is, adds an item to the cart. I am getting stuck at creating a new LineItem instance which I can push to cart. I've tried something along the lines of
new_line_item = LineItem.new(<arguments>)
but I can't find any documentation on what to supply as arguments. I have also looked into the .new() function on Variant and Product classes but I can't figure out the correct arguments to build this object either.
The end goal is to add an item to the cart with a 100% discount without using an app.
Thanks,
Jaye
The script editor is primarily used for discounting the existing items in your users cart. It does not have the ability to add new products to cart on its own. You'd need to use JavaScript in your theme to add the product to cart and then use some sort of identifier (maybe a line item property) that when seen by your script will change it to a $0 product. Not the safest thing in the world, but it'll get the job done.
*"not the safest" meaning someone who understands Shopify themes might be able to figure out the line item property and they can use JS in the console to add the product to cart and get it free. So you'll have to setup some strict logic rules in your script to only apply the discount in very specific circumstances if you don't want people to potentially abuse the free product logic.
I've just implemented something very similar. You can tag the free gift products and check product tags in Shopify Scripts. I.e.: Only give the discount, if the line item property is set and the tag is set. Then it's safe.
Are you ready to take your business to the next level? Look no further than the latest ...
By SarahF_Shopify Apr 15, 2024We’re keeping the ball rolling to make sure you’re always ahead of the game. So buckle ...
By JasonH Apr 8, 2024Portrait of Stephen positioned next to an image of planet Earth, with the Stephen's World ...
By JasonH Mar 18, 2024