简体   繁体   English

如何在不使用脚本编辑器应用程序的情况下在 shopify 首次亮相主题中添加等级折扣脚本

[英]how can i add tier discount script in shopify debut theme without using script editor app

I'm new to Shopify.我是 Shopify 的新手。 please anyone can answer.请任何人都可以回答。 just how to add a discount script in debut default theme without using any script editor app.只是如何在不使用任何脚本编辑器应用程序的情况下在首次亮相的默认主题中添加折扣脚本。 this is a script for spend X amount and get a Y% discount... I got this ruby script in 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

Is it possible to add the script without using script editor and Shopify plus app.是否可以在不使用脚本编辑器和 Shopify plus app 的情况下添加脚本。 please anyone helps me.请任何人帮助我。

No, you must use a Shopify Script App.不可以,您必须使用 Shopify 脚本应用程序。

You can't write ruby scripts in the theme code, only liquid and html/css/js is allowed there.您不能在主题代码中编写 ruby 脚本,那里只允许使用液体和 html/css/js。

If you like to have automatic discounts look into Automatic Discounts (you can have one active) https://YOUR_STORE.myshopify.com/admin/discounts/automatic .如果您想获得自动折扣,请查看自动折扣(您可以有一个有效的) https://YOUR_STORE.myshopify.com/admin/discounts/automatic But you won't be able to achieve what you are describing with the current set of discounts conditions.但是您将无法使用当前的折扣条件来实现您所描述的内容。

Correct this is not possible without the script editor app.如果没有脚本编辑器应用程序,这是不可能的。 The code runs in Shopify checkout so you cannot insert it unfortunately.该代码在 Shopify 结帐中运行,因此很遗憾您无法插入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在首次亮相的主题 shopify 的购物车页面中添加为数量下拉菜单 - Add as quantity drop down in cart page of debut theme shopify 如何在shopify的首次亮相主题中调用/cart/update.js - How to call /cart/update.js in debut theme of shopify 我们如何在不使用Script标签的情况下将我们的自定义APP代码(html代码)注入shopify产品页面 - How can we inject our custom APP code (html code) into shopify product page without using Script tag 购物 | 首次亮相主题静态标题反弹 - Shopify | Debut theme static header bounce 如何将脚本添加到 Wordpress 的主题 - How to add script to theme of Wordpress 如何将工作表列表添加到 Google App Script? - How can i add list of sheets to Google App Script? Shopify - 首次亮相主题 - 如果选择了某些变体,则显示一个文本框 - Shopify - Debut Theme - Showing A Textbox if Certain Variant is Selected 在滚动条上粘贴 header 给出了混蛋首演主题 shopify - Stick header on scroll gives jerk debut theme shopify 在Shopify首次亮相主题中将下拉列表拆分为多个列 - Split a Drop-Down Into Multiple Columns In Shopify Debut Theme 如何添加 Javascript 以更改 header 在 Shopify (区主题)中滚动时的文本颜色 - How can I add Javascript to change header text color on scroll in Shopify (District Theme)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM