我应该如何更改脚本,使其适用于收藏品,而不是Shopify中的一种产品?

问题描述:

该脚本是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 

,但我得到一个错误:

undefined method 'collection' for main (Your Cart)

undefined method'collection' for main (No Customer)

+1

没有方法(或变量)命名集合... –

这里有两件事:

  1. line_item.variant.product没有房产collections。为此,您需要使用line_item.productdocs) - 其中( ...请参见第二点)公开了product对象的所有方法和属性。

  2. 但是,在我尝试做类似于你(基于产品的折扣)的尝试时,我尝试迭代line_item.variant - 并且总是碰到undefined method 'product' for #<LineItem:0x7f9c97f9fff0>的错误。我认为“line_items在购物车脚本中访问只能在变体级别”。

所以,我不知道这是因为车只包含变种(产品/颜色/大小) - 所以我们实际上并没有能够通过产品访问line_items,只有靠变异。

我累了遍历line_item.product_id,这也会引发类似的错误。我认为我们只需要在variant级别尝试做一些冒险的事情。

我打算查看是否可以通过变体ID访问产品...返回到文档!

+0

谢谢,@ryantkelly。我通过使用标签而不是集合来获得脚本来完成我想要的功能。无论如何,我想要的集合都是使用标签作为过滤器,所以对我来说很合适。 – Andri

+0

@安德里耶,那也是我最终做的。我只是希望文档更清晰! – ryantkelly

+0

是的!奇怪的是,在液体代码中,我们可以访问产品的集合,但在Ruby代码中我们不能。 – Andri