Hoppa till innehåll
Script-exempel

Migrating a skip already discounted items script

This looks like the easiest guard in the whole library and it is the hardest. The question it asks is about the other discounts running alongside it, not about the product, and that is a different kind of question entirely.

Shopify-begränsning
Vad detta Script gjorde

It skipped any line that had already been reduced by something else.

Testa det i din egen butik

Klistra in det i Scripts-räddning och se vad som byggs

  1. Kopiera Ruby-koden med knappen bredvid vilket Script som helst på sidan.
  2. Öppna Stackable i Shopify-adminen, sedan Verktyg, sedan Scripts-räddning.
  3. Klistra in det där, läs vad verktyget säger att det ska bygga och jämför med inställningarna på den här sidan.

Scripts-räddning ingår i Pro-planen. Ingenting publiceras förrän du anger vad ditt gamla Script gav i rabatt och båda beloppen stämmer på öret.

Detta Script

Shopify-begränsning
Den ursprungliga Ruby-koden

# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
# Version 0.12.0

class Campaign
  def initialize(condition, *qualifiers)
    @condition = (condition.to_s + '?').to_sym
    @qualifiers = PostCartAmountQualifier ? [] : [] rescue qualifiers.compact
    @post_amount_qualifiers = []
    @line_item_selector = qualifiers.last unless @line_item_selector
    qualifiers.compact.each do |qualifier|
      is_multi_select = qualifier.instance_variable_get(:@conditions).is_a?(Array)
      if is_multi_select
        qualifier.instance_variable_get(:@conditions).each do |nested_q|
          @post_amount_qualifiers << nested_q if nested_q.is_a?(PostCartAmountQualifier)
          @qualifiers << qualifier
        end
      else
        @post_amount_qualifiers << qualifier if qualifier.is_a?(PostCartAmountQualifier)
        @qualifiers << qualifier
      end
    end if @qualifiers.empty?
  end
end

class Selector
end

class ReducedItemSelector < Selector
  def initialize(match_type)
    @invert = match_type == :not
  end

  def match?(line_item)
    @invert ^ line_item.discounted?
  end
end

class PercentageDiscount
  def initialize(percent, message)
    @discount = (100 - percent) / 100.0
    @message = message
  end

  def apply(line_item)
    line_item.change_line_price(line_item.line_price * @discount, message: @message)
  end
end

class ConditionalDiscount < Campaign
  def initialize(condition, customer_qualifier, cart_qualifier, line_item_selector, discount, max_discounts)
    super(condition, customer_qualifier, cart_qualifier)
    @line_item_selector = line_item_selector
    @discount = discount
    @items_to_discount = max_discounts == 0 ? nil : max_discounts
  end

  def run(cart)
    raise "Campaign requires a discount" unless @discount
    return unless qualifies?(cart)
    applicable_items = cart.line_items.select { |item| @line_item_selector.nil? || @line_item_selector.match?(item) }
    applicable_items.each do |item|
      @discount.apply(item)
    end
  end
end

CAMPAIGNS = [
  ConditionalDiscount.new(
    :all,
    nil,
    nil,
    ReducedItemSelector.new(:not),
    PercentageDiscount.new(10, "10% off full price items"),
    0
  ),
].freeze

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

Output.cart = Input.cart
Vad som händer med den här

Shopify cannot do this at checkout

Your Script checked whether an item had already been discounted by something else. Discounts at checkout all run at the same moment and cannot see each other's work, so that test could only ever be half right.

Det här är en begränsning i Shopify och inte en lucka i Stackable. Ingen rabattapp kan bygga om det i dag, och därför går länken nedan till Shopifys egen dokumentation och inte till vår.

Läs Shopifys dokumentation

Orsaksnyckel: platform.perLineAlreadyDiscounted

Det finns ingen varukorg att testa här, eftersom ingenting byggs. Att klistra in det här ger meddelandet ovan och ingen kampanj alls.

Vanliga frågor

Will Stackable rebuild this script?
No, and the reason is stated rather than hidden: Your Script checked whether an item had already been discounted by something else. Discounts at checkout all run at the same moment and cannot see each other's work, so that test could only ever be half right.

Besläktade Script-former

Scoped to a tag or vendor

A discount limited to products carrying one tag, vendor or type.

Exclude sale items

Already-marked-down products protected from a second discount.

Exclude gift cards

Gift cards left out of a discount that would otherwise have hit them.

Klistra in ditt Script och se det byggas om

Ombyggnaden stäms av mot beloppet som ditt gamla Script tog ut innan du kan publicera den.

Vi använder nödvändiga cookies för att driva den här webbplatsen, och, bara med ditt tillstånd, analyscookies för att förstå trafiken. Läs vår Cookiepolicy.