Ad

Shopify Shipping Script Discount On Customer Tag

- 1 answer

I have a Shopify Plus account in which I am attempting to add a new condition to my Shipping Script that controls what shipping methods are displayed to customers. Specifically, I want to give customers with a specific tag access to free shipping. To control that

def freeItUp
  index = -1 
  if Input.cart.customer != nil and Input.cart.customer.tags != nil 
    Input.cart.customer.tags.index do |tag|
      return tag.upcase == "ALWAYSFREE"
    end 
  end
  return index > -1
end 

shippingMethodToDelete = (condition1 or condition2 or freeItUp) ? "UPS" : "FREE"

Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
  puts shipping_rate.name
  shipping_rate.name.upcase.start_with?(shippingMethodToDelete)
end

However, I keep getting the following error

fiber required for enumerator (Your Cart)

fiber required for enumerator (Empty Cart)

I'm not terribly familiar with Ruby, but I'm using the same code block (above) in Line Item scripts with no issues. According to the documentation I believe I'm not trying to access properties that don't exist. Any thoughts/help would be greatly appreciated.

Ad

Answer

Can you try:

  def always_free_tag?
    return false if Input.cart.customer.nil? || Input.cart.customer.tags.empty?
    Input.cart.customer.tags.any? do |tag|
      tag.upcase == "ALWAYSFREE"
    end
  end

  shippingMethodToDelete = (condition1 or condition2 or always_free_tag?) ? "UPS" : "FREE"

  Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
    puts shipping_rate.name
    shipping_rate.name.upcase.start_with?(shippingMethodToDelete)
  end

Also, can you share more of the stack trace and what line it points to?

Edit: Great! Glad that worked. I think your issue is you were using Array#index which yields an index (integer) to the block (this line: Input.cart.customer.tags.index). You're then calling #upcase on Numeric which isn't a method. If you instead did this it would work as expected:

Input.cart.customer.tags.index do |i|
  return true if Input.cart.customer.tags[i].upcase == "ALWAYSFREE"
end 

The other issue is that in ruby only nil and false are falsy in ruby, so your condition in shippingMethodToDelete likely wasn't returning the correct value. Instead, I changed it so it always returns a boolean that way we can be sure ALWAYSFREE is searched for and evaluated correctly. I think you were really close but take a look at Enumerable#any for more examples.

Ad
source: stackoverflow.com
Ad