Skip to content
Scripts examples

Migrating a purchase quantity limit script

Not a discount at all, and full of the exact syntax a BOGO uses, so a careless reader turns a purchase limit into a giveaway. The reliable tell is that it deletes cart lines and never changes a price.

Different Shopify API
What the Script did

It split the excess units off a line and deleted them from the cart.

Run it against your own store

Paste it into Scripts rescue and watch what it rebuilds

  1. Copy the Ruby with the button beside any Script on this page.
  2. In your Shopify admin, open Stackable, then Tools, then Scripts rescue.
  3. Paste it there, read what the tool says it will build, and compare that with the settings on this page.

Scripts rescue is on the Pro plan. Nothing is published until you tell it what your old Script charged and the two figures match to the cent.

The Script

Different Shopify API

Danger pair D6. This file and its partner look almost identical and behave differently, so read both before you decide which one you have.

The original Ruby

# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP

class QuantityLimit < Campaign
  def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, limit_by, limit)
    super(condition, customer_qualifier, cart_qualifier, line_item_selector)
    @limit_by = limit_by
    @limit = limit
  end

  def run(cart)
    return unless qualifies?(cart)
    to_delete = []
    counts = {}
    cart.line_items.each_with_index do |item, index|
      next unless @line_item_selector.nil? || @line_item_selector.match?(item)
      key = @limit_by == :product ? item.variant.product.id : item.variant.id
      counts[key] = 0 unless counts[key]
      max_amount = @limit - counts[key]
      if max_amount <= 0
        to_delete << index
      elsif item.quantity > max_amount
        item.split(take: max_amount)
        counts[key] += max_amount
      else
        counts[key] += item.quantity
      end
    end
    to_delete.reverse.each { |index| cart.line_items.delete_at(index) }
  end
end

CAMPAIGNS = [
  QuantityLimit.new(:all, nil, nil, ProductTagSelector.new(:does, :match, ["limited"]), :variant, 2),
].freeze

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart
What happens to this one

This needs a different kind of Shopify app

Your Script blocked or limited checkout, such as a purchase limit or an age check. Shopify builds that with the Cart and Checkout Validation API, which Stackable does not ship.

This part of a Script is not a discount. Shopify builds it with a different function type, named above, which Stackable does not ship.

Read Shopify's documentation

Reason key: otherApi.cartCheckoutValidation

There is no cart to test here, because nothing is built. Pasting this produces the message above and no campaign at all.

Two files that look the same

Each pair below is one Script shape written two ways. They differ by a line or two and they do different things, so read both before deciding which one you have.

Danger pair D6

Quantity limit purchase cap (D6)

Different Shopify API

Not rebuilt. This needs a different kind of Shopify app

Lines that differ
# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
class QuantityLimit < Campaign
  def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, limit_by, limit)
    super(condition, customer_qualifier, cart_qualifier, line_item_selector)
    @limit_by = limit_by
    @limit = limit

And 26 more differing lines, on its own page.

Read Purchase quantity limit

Quantity limit first n discounted (D6)

Rebuilt

Rebuilt as a bxgy offer.

Discount on its own cart: $23.70

Lines that differ
DISCOUNT_PERCENTAGE = 30
MAX_UNITS = 2
num_to_discount = MAX_UNITS
Input.cart.line_items.each_with_index do |line_item, position|
  break if num_to_discount <= 0
  next unless line_item.variant.product.tags.include?("limited")

And 9 more differing lines, on its own page.

Read Limit the discounted units

Common questions

Will Stackable rebuild this script?
No, and the reason is stated rather than hidden: Your Script blocked or limited checkout, such as a purchase limit or an age check. Shopify builds that with the Cart and Checkout Validation API, which Stackable does not ship.

Related Script shapes

Limit the discounted units

The discount applied to the first few units only, with the rest at full price.

Remove products by country

Products that could not ship to the shopper’s country, quietly deleted.

Hide a payment method

A payment option taken off checkout under a condition.

Paste your Script and see it rebuilt

The rebuild is checked against the money your old Script charged before you can publish it.

We use essential cookies to run this site, and, only with your permission, analytics cookies to understand traffic. Read our Cookie Policy.