随着假期的临近,我的团队正在为我们的Shopifystore 开发一个脚本,以防止我们的客户在购物车中出现以下情况之一时使用折扣代码:捐赠、已经打折的商品以及符合F30 335i赠品资格的商品.以下是我目前拥有的代码:

##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

我想要的是输出的代码:"不能在购物车中使用折扣,无效的供应商:演进赛车,精密赛车"

但相反,我一直得到的是:

不能使用折扣.原因:购物车中的供应商无效:[]

推荐答案

为了澄清--我最初的问题是我收到的输出如下: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_venders"时,它没有返回NULL,但当我打印出Vendor_Message时,它返回了NULL.

因此,为了能够获取数组,将值追加到数组中,并创建用户易于阅读的整洁语句,我编写了以下代码:

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

Ruby相关问答推荐

如何通过语法将Ruby Sorbet签名突出显示为不强调的 comments ?

Ruby:这两种混入模块方法是否等效?

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

Ruby 中无法解释的撬动行为

如何从外部获取给定 lambda 的参数值,而不显式返回其绑定?

ruby `encode': "\xC3" 从 ASCII-8BIT 到 UTF-8 (Encoding::UndefinedConversionError)

如何使用 RSpec 忽略或跳过测试方法?

全新安装 RVM 和 Ruby 2.1.1 - dyld 库/路径错误

GET 和 POST 请求的相同 Rails 4 路由

如何自定义 Jekyll 的 url?

to_a 和 to_ary 有什么区别?

将数组分成 n 组 m 大小?

如何通过拆分字符的最后一次出现将字符串拆分为仅两部分?

确定Ruby的位置

使用 Ruby 将大写字符串转换为标题大小写

在本地覆盖 Vagrant 配置设置(每个开发人员)

Ruby 将 CSV 文件读取为 UTF-8 和/或将 ASCII-8Bit 编码转换为 UTF-8

为什么在 ruby​​ 中动态创建大量符号不是一个好主意(对于 2.2 之前的版本)?

Ruby 中的Monkey Patching到底是什么意思?

JavaScript Array:获取项目的范围