Shopify 脚本编辑器使用产品标签而不是 id
Shopify script editor use product tag not id
我正在使用此代码在购物车达到 X 数量时添加免费礼物(3 种不同的阈值和礼物)。脚本工作得很好,但我想用产品标签而不是产品 ID 来为产品做广告。我是 ruby 的新手,所以非常感谢任何建议。
这是脚本(我只上传了 1 个门槛,如果我把其中的 3 个门槛放上去,代码太多,我不会上传):
FREEGIFT_X = [
{
product_selector_match_type: :include,
product_selector_type: :product_id,
product_selectors: [6678361931933],
threshold: 4500,
quantity_to_discount: 1,
discount_type: :percent,
discount_amount: 100,
discount_message: 'Spend 4500 and get a free XXX',
}
]
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 subscription(line_item)
!line_item.selling_plan_id.nil?
end
def all(line_item)
true
end
end
class DiscountApplicator
def initialize(discount_type, discount_amount, discount_message)
@discount_type = discount_type
@discount_message = discount_message
@discount_amount = if discount_type == :percent
1 - (discount_amount * 0.01)
else
Money.new(cents: 100) * discount_amount
end
end
def apply(line_item)
new_line_price = if @discount_type == :percent
line_item.line_price * @discount_amount
else
[line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
end
line_item.change_line_price(new_line_price, message: @discount_message)
end
end
# DiscountLoop
class DiscountLoop
def initialize(discount_applicator)
@discount_applicator = discount_applicator
end
def loop_items(cart, line_items, num_to_discount)
line_items.each do |line_item|
break if num_to_discount <= 0
if line_item.quantity > num_to_discount
split_line_item = line_item.split(take: num_to_discount)
@discount_applicator.apply(split_line_item)
position = cart.line_items.find_index(line_item)
cart.line_items.insert(position + 1, split_line_item)
break
else
@discount_applicator.apply(line_item)
num_to_discount -= line_item.quantity
end
end
end
end
# DiscCampaign
class DiscCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart)
@campaigns.each do |campaign|
threshold = Money.new(cents: 100) * campaign[:threshold]
next if cart.subtotal_price < threshold
product_selector = ProductSelector.new(
campaign[:product_selector_match_type],
campaign[:product_selector_type],
campaign[:product_selectors],
)
eligible_items = cart.line_items.select { |line_item| product_selector.match?(line_item) }
next if eligible_items.nil?
eligible_items = eligible_items.sort_by { |line_item| line_item.variant.price }
num_to_discount = campaign[:quantity_to_discount]
cart_total = cart.subtotal_price
eligible_items.each do |line_item|
break if num_to_discount <= 0
if line_item.quantity > num_to_discount
cart_total -= line_item.variant.price * num_to_discount
break
else
cart_total -= line_item.line_price
num_to_discount -= line_item.quantity
end
end
next if cart_total < threshold
discount_applicator = discount_applicator = DiscountApplicator.new(
campaign[:discount_type],
campaign[:discount_amount],
campaign[:discount_message]
)
discount_loop = DiscountLoop.new(discount_applicator)
discount_loop.loop_items(cart, eligible_items, campaign[:quantity_to_discount])
end
end
end
CAMPAIGNS = [
DiscCampaign.new(FREEGIFT_X),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cart
Shopify 实际上有 an example 个。
product_selector_type
必须是 :tag
并且您可以传递一个字符串数组作为 product_selectors
。
从他们的文档中复制粘贴:
PRODUCT_DISCOUNT_TIERS = [
{
product_selector_match_type: :include,
product_selector_type: :tag,
product_selectors: ["your_tag"],
我正在使用此代码在购物车达到 X 数量时添加免费礼物(3 种不同的阈值和礼物)。脚本工作得很好,但我想用产品标签而不是产品 ID 来为产品做广告。我是 ruby 的新手,所以非常感谢任何建议。
这是脚本(我只上传了 1 个门槛,如果我把其中的 3 个门槛放上去,代码太多,我不会上传):
FREEGIFT_X = [
{
product_selector_match_type: :include,
product_selector_type: :product_id,
product_selectors: [6678361931933],
threshold: 4500,
quantity_to_discount: 1,
discount_type: :percent,
discount_amount: 100,
discount_message: 'Spend 4500 and get a free XXX',
}
]
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 subscription(line_item)
!line_item.selling_plan_id.nil?
end
def all(line_item)
true
end
end
class DiscountApplicator
def initialize(discount_type, discount_amount, discount_message)
@discount_type = discount_type
@discount_message = discount_message
@discount_amount = if discount_type == :percent
1 - (discount_amount * 0.01)
else
Money.new(cents: 100) * discount_amount
end
end
def apply(line_item)
new_line_price = if @discount_type == :percent
line_item.line_price * @discount_amount
else
[line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
end
line_item.change_line_price(new_line_price, message: @discount_message)
end
end
# DiscountLoop
class DiscountLoop
def initialize(discount_applicator)
@discount_applicator = discount_applicator
end
def loop_items(cart, line_items, num_to_discount)
line_items.each do |line_item|
break if num_to_discount <= 0
if line_item.quantity > num_to_discount
split_line_item = line_item.split(take: num_to_discount)
@discount_applicator.apply(split_line_item)
position = cart.line_items.find_index(line_item)
cart.line_items.insert(position + 1, split_line_item)
break
else
@discount_applicator.apply(line_item)
num_to_discount -= line_item.quantity
end
end
end
end
# DiscCampaign
class DiscCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart)
@campaigns.each do |campaign|
threshold = Money.new(cents: 100) * campaign[:threshold]
next if cart.subtotal_price < threshold
product_selector = ProductSelector.new(
campaign[:product_selector_match_type],
campaign[:product_selector_type],
campaign[:product_selectors],
)
eligible_items = cart.line_items.select { |line_item| product_selector.match?(line_item) }
next if eligible_items.nil?
eligible_items = eligible_items.sort_by { |line_item| line_item.variant.price }
num_to_discount = campaign[:quantity_to_discount]
cart_total = cart.subtotal_price
eligible_items.each do |line_item|
break if num_to_discount <= 0
if line_item.quantity > num_to_discount
cart_total -= line_item.variant.price * num_to_discount
break
else
cart_total -= line_item.line_price
num_to_discount -= line_item.quantity
end
end
next if cart_total < threshold
discount_applicator = discount_applicator = DiscountApplicator.new(
campaign[:discount_type],
campaign[:discount_amount],
campaign[:discount_message]
)
discount_loop = DiscountLoop.new(discount_applicator)
discount_loop.loop_items(cart, eligible_items, campaign[:quantity_to_discount])
end
end
end
CAMPAIGNS = [
DiscCampaign.new(FREEGIFT_X),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cart
Shopify 实际上有 an example 个。
product_selector_type
必须是 :tag
并且您可以传递一个字符串数组作为 product_selectors
。
从他们的文档中复制粘贴:
PRODUCT_DISCOUNT_TIERS = [
{
product_selector_match_type: :include,
product_selector_type: :tag,
product_selectors: ["your_tag"],