Personalized checkout and custom promotions with Shopify Scripts
I'm writing a script that will apply free shipping if the cart contains a specific item and a discount code is used. Here is my script
if Input.cart.discount_code && Input.cart.discount_code.code == "FREEITEM" ELIGIBLE_SERVICE = "Standard Ground Shipping" ELIGIBLE_ITEM = "424492626116" Input.cart.line_items.each do |line_item| if line_item.variant.product.id == ELIGIBLE_ITEM Input.shipping_rates.each do |shipping_rate| if shipping_rate.name == ELIGIBLE_SERVICES shipping_rate.apply_discount(shipping_rate.price, message: "Free shipping for this product!") end end end end end Output.shipping_rates = Input.shipping_rates
However, it only runs when there are multiple items in the cart. It should work when the specific item is added, even if it's the only item added.
Hi @KolbySisk ,
Did you select "line_item" as the type of script in Script Editor? Also at which point do you apply the discount because without a discount the remainder of the code would have never executed.
Best,
Sam
User | RANK |
---|---|
2 | |
2 | |
2 | |
2 | |
2 |
Thank you to everyone who participated in our AMA with Klaviyo. It was great to see so man...
By Jacqui May 30, 2023Photo by Marco Verch Sales channels on Shopify are various platforms where you can sell...
By Ollie May 25, 2023Summary of EventsBeginning in January of 2023, some merchants reported seeing a large amo...
By Trevor May 15, 2023