【问题标题】:how can i add tier discount script in shopify debut theme without using script editor app如何在不使用脚本编辑器应用程序的情况下在 shopify 首次亮相主题中添加等级折扣脚本
【发布时间】:2020-08-13 06:55:57
【问题描述】:

我是 Shopify 的新手。请任何人都可以回答。只是如何在不使用任何脚本编辑器应用程序的情况下在首次亮相的默认主题中添加折扣脚本。 这是一个花费 X 金额并获得 Y% 折扣的脚本...我在 Shopify 中获得了这个 ruby​​ 脚本。

  PRODUCT_DISCOUNT_TIERS = [
    {
   product_selector_match_type: :include,
   product_selector_type: :tag,
     product_selectors: ["your_tag"],
     tiers: [
       {
      quantity: 2,
    discount_type: :percent,
    discount_amount: 10,
    discount_message: '10% off for 2+',
  },
  {
    quantity: 5,
    discount_type: :percent,
    discount_amount: 15,
    discount_message: '15% off for 5+',
  },
],
  },
 ]
   class ProductSelector
    def initialize(match_type, selector_type, selectors)
    @match_type = match_type
    @comparator = match_type == :include ? 'any?' : 'none?'
    @selector_type = selector_type
    @selectors = selectors
   end

   def match?(line_item)
    if self.respond_to?(@selector_type)
      self.send(@selector_type, line_item)
    else
  raise RuntimeError.new('Invalid product selector type')
  end
end

  def tag(line_item)
   product_tags = line_item.variant.product.tags.map { |tag| tag.downcase.strip }
   @selectors = @selectors.map { |selector| selector.downcase.strip }
   (@selectors & product_tags).send(@comparator)
  end

   def type(line_item)
   @selectors = @selectors.map { |selector| selector.downcase.strip }
   (@match_type == :include) == @selectors.include? 
   (line_item.variant.product.product_type.downcase.strip)
  end

  def vendor(line_item)
@selectors = @selectors.map { |selector| selector.downcase.strip }
(@match_type == :include) == @selectors.include? 
 (line_item.variant.product.vendor.downcase.strip)
 end

 def product_id(line_item)
(@match_type == :include) == @selectors.include?(line_item.variant.product.id)
 end

 def variant_id(line_item)
(@match_type == :include) == @selectors.include?(line_item.variant.id)
 end

 def all(line_item)
  true
 end
  end
  class TieredPricingCampaign
    def initialize(campaigns)
    @campaigns = campaigns
   end

  def run(cart)
   @campaigns.each do |campaign|
  product_selector = ProductSelector.new(
    campaign[:product_selector_match_type],
    campaign[:product_selector_type],
    campaign[:product_selectors],
  )

  applicable_items = cart.line_items.select { |line_item| product_selector.match?(line_item) 
 }

  next if applicable_items.nil?

  total_applicable_quantity = applicable_items.map(&:quantity).reduce(0, :+)
  tiers = campaign[:tiers].sort_by { |tier| tier[:quantity] }.reverse
  applicable_tier = tiers.find { |tier| tier[:quantity] <= total_applicable_quantity }

  next if applicable_tier.nil?

  discount_applicator = DiscountApplicator.new(
    applicable_tier[:discount_type],
    applicable_tier[:discount_amount],
    applicable_tier[:discount_message]
  )

  applicable_items.each do |line_item|
    discount_applicator.apply(line_item)
  end
end
end
end

  CAMPAIGNS = [
  TieredPricingCampaign.new(PRODUCT_DISCOUNT_TIERS),
  ]

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

Output.cart = Input.cart

是否可以在不使用脚本编辑器和 Shopify plus 应用的情况下添加脚本。请任何人帮助我。

【问题讨论】:

    标签: javascript ruby ajax shopify liquid


    【解决方案1】:

    不,您必须使用 Shopify 脚本应用程序。

    主题代码中不能写ruby脚本,只允许使用liquid和html/css/js。

    如果您想获得自动折扣,请查看自动折扣(您可以激活一个)https://YOUR_STORE.myshopify.com/admin/discounts/automatic。但是您将无法在当前的折扣条件下实现您所描述的目标。

    【讨论】:

    • 谢谢@drip。还有一个问题。是否可以通过支出为分层折扣编写代码?使用js或其他技术
    • 你不能用 JS 创建折扣。但是您可以为不同的支出创建多重折扣,并使用流动性添加一个隐藏的折扣字段,该字段将传递到结帐。例如 {% if cart.total_price &gt; 1000 %}&lt;input type="hidden" name="discount" value="1DOLAR_DISCOUNT" /&gt;{% endif %} 这必须添加到 CART 表单中才能使用,并且您必须有一个名为 1DOLAR_DISCOUNT 的折扣。您必须创建一个if/else if 检查您的所有层级以及所有折扣。 (但如果没有 Shopify 脚本,它们就不能根据确切的价格动态)
    • 你能给我举个例子吗
    【解决方案2】:

    如果没有脚本编辑器应用程序,这是不可能的。该代码在 Shopify 结帐中运行,因此很遗憾您无法插入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多