[英]How should I change his script so that it works for collections and not just one product in Shopify?
该脚本是Shopify中脚本编辑器应用程序随附的模板。 我需要使其工作,以便如果您从一个系列中购买一种产品,那么从该系列中可以获得另一种免费的产品。 该脚本仅适用于购买相同产品。 这是脚本:
PAID_ITEM_COUNT = 2
DISCOUNTED_ITEM_COUNT = 1
# Returns the integer amount of items that must be discounted next
# given the amount of items seen
#
def discounted_items_to_find(total_items_seen, discounted_items_seen)
Integer(total_items_seen / (PAID_ITEM_COUNT + DISCOUNTED_ITEM_COUNT) * DISCOUNTED_ITEM_COUNT) - discounted_items_seen
end
# Partitions the items and returns the items that are to be discounted.
#
# Arguments
# ---------
#
# * cart
# The cart to which split items will be added (typically Input.cart).
#
# * line_items
# The selected items that are applicable for the campaign.
#
def partition(cart, line_items)
# Sort the items by price from high to low
sorted_items = line_items.sort_by{|line_item| line_item.variant.price}.reverse
# Create an array of items to return
discounted_items = []
# Keep counters of items seen and discounted, to avoid having to recalculate on each iteration
total_items_seen = 0
discounted_items_seen = 0
# Loop over all the items and find those to be discounted
sorted_items.each do |line_item|
total_items_seen += line_item.quantity
# After incrementing total_items_seen, see if any items must be discounted
count = discounted_items_to_find(total_items_seen, discounted_items_seen)
# If there are none, skip to the next item
next if count <= 0
if count >= line_item.quantity
# If the full item quantity must be discounted, add it to the items to return
# and increment the count of discounted items
discounted_items.push(line_item)
discounted_items_seen += line_item.quantity
else
# If only part of the item must be discounted, split the item
discounted_item = line_item.split(take: count)
# Insert the newly-created item in the cart, right after the original item
position = cart.line_items.find_index(line_item)
cart.line_items.insert(position + 1, discounted_item)
# Add it to the list of items to return
discounted_items.push(discounted_item)
discounted_items_seen += discounted_item.quantity
end
end
# Return the items to be discounted
discounted_items
end
eligible_items = Input.cart.line_items.select do |line_item|
product = line_item.variant.product
!product.gift_card? && product.id == 11380899340
end
discounted_line_items = partition(Input.cart, eligible_items)
discounted_line_items.each do |line_item|
line_item.change_line_price(Money.zero, message: "Buy one Bolur, get one Bolur free")
end
Output.cart = Input.cart
我尝试更改,似乎是相关的代码:
eligible_items = Input.cart.line_items.select do |line_item|
product = line_item.variant.product
!product.gift_card? && product.id == 11380899340
end
对此:
eligible_items = Input.cart.line_items.select do |line_item|
product = line_item.variant.product
!product.gift_card? && **collection.id** == 123
end
但我得到一个错误:
未定义的方法“收集”为主(您的购物车)
未定义方法“集合”(主要)(无客户)
这里有两件事:
line_item.variant.product
没有属性collections
。 为此,您想使用line_item.product
( docs )–( 应该 ...请参见第二点)公开product
对象的所有方法和属性。
但是,在尝试做类似的事情(基于产品的折扣)时,我尝试遍历line_item.variant –并始终遇到undefined method 'product' for #<LineItem:0x7f9c97f9fff0>
的undefined method 'product' for #<LineItem:0x7f9c97f9fff0>
的错误。 我解释为“在购物车脚本中访问的line_items
只能处于变体级别”。
所以,我想知道这是否是因为购物车仅包含变体(产品/颜色/尺寸),所以我们实际上不能按产品访问line_items
,而只能按变体访问。
我厌倦了遍历line_item.product_id
,这也会引发类似的错误。 我认为我们只需要尝试在variant
级别上做一些棘手的事情。
我将查看是否可以通过变体ID访问产品...返回文档!
实际上,您无法进行收集,因此您需要修改脚本以使用产品类型或标签。 该脚本需要进行大量修改才能用于多种产品,而不是同一产品的多个产品
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.