What's your biggest current challenge? Have your say in Community Polls along the right column.

Script Editor - Limiting product quantity script issue

Script Editor - Limiting product quantity script issue

CakewalkAlyssa
Shopify Partner
24 3 4

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!

Check out Simple Subscriptions for a complete recurring orders and subscriptions solution for your store. Click here to see our demo store.
Replies 4 (4)

LucaHolland
Shopify Partner
2 0 0

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.

CakewalkAlyssa
Shopify Partner
24 3 4

Hi @LucaHolland 

 

Unfortunately, no. I just accepted there was a bug and so far there hasn't really been customer issues with it. 

Check out Simple Subscriptions for a complete recurring orders and subscriptions solution for your store. Click here to see our demo store.

Michel_Arteta
Shopify Partner
37 2 34

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. 

 

  1. The code loops through each line item in the cart to check if it has an "lqty" tag.
  2. If the line item has the tag, the code retrieves the maximum quantity limit from the tag.
  3. If the quantity of the line item exceeds the limit, the code sets the quantity of the line item to the limit and adds a property to the line item indicating that the maximum quantity limit has been reached.
  4. The code then moves on to the next line item in the cart.
  5. See code here https://gist.github.com/michelarteta/15db85239d95cd6a07d89e2bfc374268

 

# 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!

jcollins
New Member
4 0 0

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?