Home > Mobile >  How to display array values in Shopify Script Editor?
How to display array values in Shopify Script Editor?

Time:11-19

With the holidays coming up, my team is working on developing a script for our Shopify store that prevents our customers from using discount codes if they have one of the following in their cart : Donations, Items already on sale, and items that qualify for a F30 335i giveaway. Here is the code I currently have :

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

Currently with this output, I can get the error message I want -- but only when it is a string.

The last piece to my puzzle is needed to be able to display the invalid vendors that the customer has in their cart, shown here :


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

What I would like to have is code that outputs : "Cannot use discount, Invalid vendors in cart : Evolution racewerks, Precision Raceworks"

But instead what I keep on getting is :

Cannot use Discount. Reason(s) : Invalid vendors in cart : []

CodePudding user response:

So to clarify -- my initial issue was that the output I was receiving was the following : Cannot use Discount. Reason(s) : Invalid vendors in cart : [] -- when in reality what I needed to be displayed in the cart was : "Invalid vendors in cart : Evolution racewerks, Precision raceworks..." What I also wanted to avoid was the content of the array being displayed, as in -- Invalid vendors in cart : ["evolution racewerks', ...] -- SO upon further research, what I discovered was that my issue was in this line here :

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

Im not entirely sure why this wouldn't work, but when I printed out "list_of_invalid_vendors", it didn't return null, but when I printed out vendor_message, it did.

So for me to be able to take an array, append values to it, and create a clean looking sentence that was easy to read for my user, i wrote the following :

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