跳转到主要内容
Script 示例

Migrating a discounted cart subtotal script

Two scripts can look identical here and mean opposite things. One measures what its own earlier lines left behind; the other measures what somebody else’s discount left behind, and only the second is out of anyone’s hands.

Shopify 平台限制
这个 Script 做了什么

It subtracted the shopper’s discount code from the subtotal first, then compared what was left against its threshold.

在您自己的店铺里试一试

把它粘贴到 Scripts 救援,看看会重建成什么

  1. 用本页任意 Script 旁边的按钮复制 Ruby 代码。
  2. 在 Shopify 后台打开 Stackable,依次进入工具、Scripts 救援。
  3. 粘贴进去,读一读工具说它会创建什么,再和本页的设置逐项对照。

Scripts 救援属于 Pro 套餐。在您填写旧 Script 的折扣金额、且两个数字精确到最小货币单位完全一致之前,不会发布任何内容。

Script 本体

Shopify 平台限制
原始 Ruby

# GENERATED BY THE SHOPIFY SCRIPT CREATOR APP
# Version 0.12.0

class Qualifier
  def compare_amounts(compare, comparison_type, compare_to)
    case comparison_type
      when :greater_than
        return compare > compare_to
      when :greater_than_or_equal
        return compare >= compare_to
      else
        raise "Invalid comparison type"
    end
  end
end

class PostCartAmountQualifier < Qualifier
  def initialize(comparison_type, amount)
    @comparison_type = comparison_type
    @amount = Money.new(cents: amount * 100)
  end

  def match?(cart, selector = nil)
    total = cart.subtotal_price
    compare_amounts(total, @comparison_type, @amount)
  end
end

class PostCartAmountIncDiscCodeQualifier < PostCartAmountQualifier
  def initialize(comparison_type, amount)
    super(comparison_type, amount)
  end

  def match?(cart, selector = nil)
    total =
    case cart.discount_code
      when CartDiscount::Percentage
        if cart.subtotal_price >= cart.discount_code.minimum_order_amount
          cart_subtotal_without_gc = cart.line_items.reduce(Money.zero) do |total, item|
            total + (item.variant.product.gift_card? ? Money.zero : item.line_price)
          end
          gift_card_amount = cart.subtotal_price - cart_subtotal_without_gc
          cart_subtotal_without_gc * ((Decimal.new(100) - cart.discount_code.percentage) / 100) + gift_card_amount
        else
          cart.subtotal_price
        end
      when CartDiscount::FixedAmount
        if cart.subtotal_price >= cart.discount_code.minimum_order_amount
          [cart.subtotal_price - cart.discount_code.amount, Money.zero].max
        else
          cart.subtotal_price
        end
      else
        cart.subtotal_price
    end
    compare_amounts(total, @comparison_type, @amount)
  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)
    cart.line_items.each do |item|
      @discount.apply(item)
    end
  end
end

CAMPAIGNS = [
  ConditionalDiscount.new(
    :all,
    nil,
    PostCartAmountIncDiscCodeQualifier.new(:greater_than_or_equal, 100),
    nil,
    PercentageDiscount.new(5, "5% off orders over $100 after discounts"),
    0
  ),
].freeze

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

Output.cart = Input.cart
这一个会怎样

Shopify cannot do this at checkout

Your Script measured its threshold against a subtotal that another discount had already reduced. Discounts at checkout all run at the same moment and cannot see each other's work, so we only ever see the original amounts.

这是 Shopify 的限制,不是 Stackable 的缺失。目前没有任何折扣应用能重建它,所以下面的链接指向 Shopify 自己的文档,而不是我们的。

阅读 Shopify 官方文档

原因键: platform.postDiscountSubtotal

这里没有可供验证的购物车,因为什么都不会生成。粘贴它只会得到上面的提示,不会创建任何活动。

常见问题

Will Stackable rebuild this script?
No, and the reason is stated rather than hidden: Your Script measured its threshold against a subtotal that another discount had already reduced. Discounts at checkout all run at the same moment and cannot see each other's work, so we only ever see the original amounts.

相关的 Script 类型

Spend threshold, percentage

A percentage that unlocks once the cart is worth enough.

Spend threshold, amount off

A flat sum off the order once the cart crosses a value.

粘贴您的 Script,看看它如何被重建

在您发布之前,重建结果会与原来的 Script 收取的金额进行核对。

我们使用必要 Cookie 来运行本网站,并仅在获得你许可的情况下,使用分析 Cookie 来了解访问情况。详见我们的 Cookie 政策.