Personalized checkout and custom promotions with Shopify Scripts
Hi, I am looking for a way to restrict product purchase limit at the checkout page. Been in touch with Shopify support and they suggested this script example, which worked well.
But the problem here is it only works for one combination. Eg, right now we have the follow tags: maxQty_1, maxQty_2, maxQty_12, maxQty24 and a few more.
So to get it working for all these tags, I had to duplicate the QUANTITY_LIMITS block of code and each block would have product_selector_type value set to tag, product_selectors value set to the tag name eg "maxQty_2" and quantity_allowed value as the suffix of the tag which in this example would be "2".
Eg code block:
QUANTITY_LIMITS_2 = {
enable: true,
campaigns: [
{
product_selector_match_type: :include,
product_selector_type: :tag,
product_selectors: ["maxQty_2"],
variant_level_limit: true,
quantity_allowed: 2,
},
]
}
I also had to call all of these QUANTITY_LIMITS_X blocks within the CAMPAIGNS block at the bottom of the script, like:
CAMPAIGNS = [
ProductQuantityLimitCampaign.new(
QUANTITY_LIMITS_1[:enable],
QUANTITY_LIMITS_1[:campaigns],
),
ProductQuantityLimitCampaign.new(
QUANTITY_LIMITS_2[:enable],
QUANTITY_LIMITS_2[:campaigns],
),
ProductQuantityLimitCampaign.new(
QUANTITY_LIMITS_12[:enable],
QUANTITY_LIMITS_12[:campaigns],
),
ProductQuantityLimitCampaign.new(
QUANTITY_LIMITS_24[:enable],
QUANTITY_LIMITS_24[:campaigns],
),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cart
As you can tell, when the number of tags/rules become more, the script will become bloated. And the problem here is the campaign ends up looping a lot and depending on the cart size and items in the cart of a customer, I get this error:
InstructionQuotaExceeded | Your script exceeded the cpu limit. |
Shopify support did suggest I loop over the tags first and split the tag name from the suffix value and then use them instead of having several blocks of code. I have never worked with Ruby before this but I was able to run a loop on the line items, store the full tag name and its suffix is variables and print in the console... Here's the code snippet:
Input.cart.line_items.each do |line_item|
line_item.variant.product.tags.each do |tag|
if tag.include?('maxQty_')
fullTag = tag
msg = 'it has a maxQty_ tag!!!'
tagStart,maxVal = tag.split('_', 2)
maxVal.to_i
puts msg
puts 'fullTag is = ' + fullTag
puts 'tagStart is ' + tagStart
puts 'MaxVal is = ' + maxVal
end
end
end
Now the problem is, when I try to put the QUANTITY_LIMITS_X block of code inside the loop, I am getting an error and the script won't compile. And I'm not able to use the variables within the block of code either as that too throws an error. I'm not really sure what I am doing wrong here or if this is the right way to go about this.. The CPU max limit error is worrying! So far I think it's only happened when too many products with a "maxQty_" tag is in the cart. Should also note that this script also handles some quantity breaks. At the moment the script runs with multiple blocks of code(i have kept the tags loop one in drafts as it does not work) which is not efficient. Any help or advice would be really appreciated! Thanks a lot for your time!
Hey Community 👋 Did you know that March 15th is National Everything You Think Is W...
By JasonH Apr 1, 2025Discover how to increase the efficiency of commerce operations with Shopify Academy's l...
By Jacqui Mar 26, 2025Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025