ruby 如何在Shopify脚本编辑器中显示数组值?

s5a0g9ez  于 2022-11-22  发布在  Ruby
关注(0)|答案(1)|浏览(139)

随着假期的到来,我的团队正在为我们的Shopify商店开发一个脚本,防止我们的客户使用折扣码,如果他们有以下之一在他们的购物车:捐赠、已在销售的商品以及符合F30 335 i赠品资格的商品。以下是我目前拥有的代码:

##disable discount codes##
error_message = 'Cannot use Discount. Reason(s) : '
marzano_donation_id = 6723597598805
marzano_donation = false
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
puts product.id
if Input.cart.discount_code != nil 
    # only runs if customer has discount code
  if product.id == marzano_donation_id
    # marzano_donation = true
    marzano_error = "Marzano's Donation in cart, "
    error_message.concat(marzano_error)
        # Input.cart.discount_code.reject({message: "1"})
        
end
end
end


list_of_invalid_vendors = []
vendor_message = "Invalid vendors in cart : #{list_of_invalid_vendors}"
contains_vendor = false

vendor_list = ["Evolution Racewerks", "Kies Giveaway Items", "Precision Raceworks"]
Input.cart.line_items.each do |line_item|
vendor = line_item.variant.product.vendor
if vendor_list.include? vendor

    list_of_invalid_vendors.append(vendor)
    if Input.cart.discount_code != nil
      error_message.concat(vendor_message)
      puts list_of_invalid_vendors
      puts vendor_message
    end

  puts list_of_invalid_vendors
end

end
puts contains_vendor


contains_giveaway = false
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next unless product.tags.include?('Kies Giveaway Items')
# Input.cart.discount_code.reject({message: "3"})
    # contains_giveaway = true
    giveaway_error = " Cannot use discount code on Kies Giveaway Items, "
    error_message.concat(giveaway_error)
end
puts contains_giveaway
puts "123"

if Input.cart.discount_code != nil
Input.cart.discount_code.reject({message: "#{error_message}"})
end 

Output.cart = Input.cart

目前,通过这个输出,我可以得到我想要的错误消息--但只有当它是字符串时。
最后一个问题是要显示客户购物车中的无效供应商,如下所示:

list_of_invalid_vendors = []
vendor_message = "Invalid vendors in cart : #{list_of_invalid_vendors}"
contains_vendor = false

vendor_list = ["Evolution Racewerks", "Kies Giveaway Items", "Precision Raceworks"]
Input.cart.line_items.each do |line_item|
vendor = line_item.variant.product.vendor
if vendor_list.include? vendor

    list_of_invalid_vendors.append(vendor)
    if Input.cart.discount_code != nil
      error_message.concat(vendor_message)
      puts list_of_invalid_vendors
      puts vendor_message
    end

  puts list_of_invalid_vendors
end

end

我想要的是输出以下内容的代码:“无法使用折扣,购物车中的供应商无效:进化赛车,精密赛车”
但我得到的却是:
无法使用折扣。原因:购物车中的供应商无效:[]

ztigrdn8

ztigrdn81#

所以澄清一下--我最初的问题是我收到的输出如下:Cannot use Discount. Reason(s) : Invalid vendors in cart : []--实际上我需要在购物车中显示的内容是:"Invalid vendors in cart : Evolution racewerks, Precision raceworks..."我还想避免显示数组的内容,如--Invalid vendors in cart : ["evolution racewerks', ...]--因此,经过进一步研究,我发现问题出在下面这一行中:

vendor_message = "Invalid vendors in cart : #{list_of_invalid_vendors}"

我不完全确定为什么这不起作用,但是当我输出“list_of_invalid_vendors”时,它没有返回null,但是当我输出vendor_message时,它返回了null。
因此,为了能够接受一个数组,向其追加值,并创建一个外观整洁、易于用户阅读的句子,我编写了以下代码:

invalid_vendors = invalid_vendors.join(', ')
vendor_message = "Black Friday Vendors in Cart : #{invalid_vendors}, "
error_message.concat(vendor_message)

相关问题