Undefined method for main object - script for checking shipping address

Tourist
27 0 2

Using Fulfillment by Amazon we have to limit PO Box addresses to Standard Shipping only.

Script flow

  • get address1 as upcase string set to "addressline1" variable
  • build array of "PO Box" permutations (i.e. P.O. BOX, P.O.BOX, ...)
  • if array contains address1 hide shipping options
  • else return all

I'm getting an error in the if check though "undefined method 'addressline1' for main".  I've already tried playing with the order in which things are declared, and checked as I thought maybe the variable doesn't exist yet when the if statement is run, but that didn't remove the error.  Please see below for code block as it's a very simple script.

Any advice would be greatly appreciated.

shippingaddress1 = Input.cart.shipping_address
addressline1 = shippingaddress1.address1.upcase
checkpobox = ['P.O. Box','PO BOX','POBOX','P.O BOX','PO. BOX','P.O.BOX']

if checkpobox.include?(adressline1)
  Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
    shipping_rate.name.include?("Expedited") || shipping_rate.name.include?("Priority")
  end
else
  Output.shipping_rates = Input.shipping_rates
end

 

0 Likes
Tourist
4 0 2

Hi A-aron,

This adjustment work for me. I was having a similar issue with Payment Scripts and discovered Shopify Scripts doesn't like to run "do" enclosed within an if and vice versa.

Hope this helps!

shippingaddress1 = Input.cart.shipping_address.address1.upcase
checkpobox = ['P.O. Box','PO BOX','POBOX','P.O BOX','PO. BOX','P.O.BOX']

#define hideship based on checkpobox. If so, hide shipping options, otherwise allow all
if checkpobox.include?(shippingaddress1)
  hideship = ['Expedited','Priority']
else
  hideship = ['None']
end

#With hideship defined, now we loop through shipping rates to delete those that match
  Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
    hideship.any? { |hideship| shipping_rate.name.include? hideship }
  end
2 Likes
Tourist
27 0 2

Thank you for the insight.

I also appreciated a few additional lessons for posterity:

1) simplify variable creation section

2) simplify output method by simply appending to front of input loop

0 Likes
Tourist
4 0 2

Hi A-ron,

I found a snag.  While this method works to find one variable, it seems to fail with an array.  When I tested, I put in a specific instance "PO BOX", which worked... but "PO BOX 727" failed.  I'm thinking there should be a loop to iterate through the array until a include? match is found, then set the hideship appropriately and exit the loop. 

I found this on stackoverflow, which should provide a solution, but it was a little over my head... especially the one that mentions " Ruby has 11 methods to find elements in an array... " =)
https://stackoverflow.com/questions/1986386/check-if-a-value-exists-in-an-array-in-ruby

0 Likes
Tourist
27 0 2

I ended up modifying the shippingaddress1 variable to strip numbers and remove spaces to minimize the "variability" of the variable, and it seems to work for all tested cases so far, including "PO BOX 727"

include? method should be able to step through an array checking one at a time against the value expected.

 

0 Likes
Tourist
4 0 2

Would you mind posting the code for my posterity?  And what about P.O. BOX or PO BOX 11A?

I realized the solution was staring me in the face... (I'm also assuming you found the first 'P.O. Box' of checkpobox needs to be 'P.O. BOX'):

shipaddy1 = Input.cart.shipping_address.address1.upcase
checkpobox = ['P.O. BOX','PO BOX','POBOX','P.O BOX','PO. BOX','P.O.BOX']

if checkpobox.any? { |letter| shipaddy1.include? letter }
  hideship = ['FREE']
else
  hideship = ['NONE']
end

#With hideship defined, now we loop through shipping rates to delete those that match
  Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
    hideship.any? { |hideship| shipping_rate.name.include? hideship }
  end

 

0 Likes
Highlighted
Tourist
27 0 2

Sure thing!  If a number is in the end it shouldn't matter as I'm still looking for includes string meaning if the string occurs without it being exact.  I'm sure we could make this even cleaner though by stripping "." and spaces.

 

#Exclude Expedited and Priority options when PO Box is present
shippingaddress1 = Input.cart.shipping_address.address1.upcase.split[0...-1].join ' '
checkpobox = ['PO BOX','POBOX','P.O BOX','PO. BOX','P.O.BOX','P.O. BOX','P.0. BOX','P.OBOX']
puts shippingaddress1
#define hideship based on checkpobox. If so, hide shipping options, otherwise allow all
if checkpobox.include?(shippingaddress1)
  hideship = ['Expedited','Priority']
else
  hideship = ['None']
end

#With hideship defined, now we loop through shipping rates to delete those that match
Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
  hideship.any? { |hideship| shipping_rate.name.include?(hideship) }
end

 

1 Like
Shopify Partner
5 0 0

I know this is old, But this is awesome. And works great! Thanks so much for this! 

0 Likes