[英]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.