Personalized checkout and custom promotions with Shopify Scripts
So I have a line item script that limits a product quantity. It works in the sense that any excess quantity is removed from the cart but then if i try to remove the item from the cart, either from decrementing the count in the quantity selector or from the trash can I get the 'You can only add 1 of this item to your cart.' message again and the item remains in the cart until I try to remove it again.
My script is based on https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/line-item-scripts#limit-... and I have reproduced this issue using this exact script.
repro steps using above linked script and max 1 quantity:
add item to cart
increase quantity to 2 -> excess removed, quantity shows as 1 and You can only add 1 of this item to your cart. messaging appears
decrease quantity to 0 or use trash icon -> quantity briefly shows as 0 then shows as 1 and You can only add 1 of this item to your cart. messaging remains
try to remove again -> item is removed from cart
Any help resolving this issue would be appreciated!
Hi,
Just to ask, did you find a solution to this? I realise that its nearly a year on, but I'm having a similar issue.
I can limit the quantity going into the cart, but when that item is later removed it is re-added to the cart.
Hi @LucaHolland
Unfortunately, no. I just accepted there was a bug and so far there hasn't really been customer issues with it.
Have you tried using instance_variable_set to limit the quantity of items in your cart? It's pretty straightforward - See the script attached; it checks for a tag on each product in the cart that specifies a maximum quantity limit, and then modifies the quantity of the product if it exceeds that limit.
# Loop through the line items in the cart
Input.cart.line_items.each do |line_item|
# Check if the line item has an "lqty" tag
tag_limit = nil
line_item.variant.product.tags.each do |tag|
if tag.start_with?("lqty:")
tag_limit = tag.split(":").last.to_i
if line_item.quantity.to_i > tag_limit.to_i
line_item.instance_variable_set(:@quantity, tag_limit.to_i)
line_item.change_properties({ "Max Quantity Limit Reached" => tag_limit.to_i}, message: "Adding properties")
break
end
break
end
end
end
Output.cart = Input.cart
Give it a try and let me know if you have any questions!
Wondering if there's a way to modify this script so that the max quantity applies to any item in the cart? So for example, if I want to set a limit of 7 total stickers per cart, and a customer puts 7 black stickers and 7 red stickers, then 7 total (either red or black) would be removed and the limit message would show. The customer could only buy 7 total of this product type, not 7 red and 7 black. Any ideas?
User | RANK |
---|---|
4 | |
2 | |
2 | |
2 | |
2 |
Make the shift from discounts to donations, and witness your business not only thrive fina...
By Holly Dec 4, 2023On our Shopify Expert Marketplace, you can find many trusted third party developers and fr...
By Arno Nov 27, 2023You've downloaded the Search & Discovery app from the Shopify App store, and as you're ...
By Skye Nov 8, 2023