gpt4 book ai didi

ruby - Shopify 脚本编辑器使用产品标签而不是 ID

转载 作者:行者123 更新时间:2023-12-04 07:44:10 24 4
gpt4 key购买 nike

当购物车达到 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"],

关于ruby - Shopify 脚本编辑器使用产品标签而不是 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67282750/

24 4 0