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