PO BOX checker script production error

Solved
Dominic_Nadeau
Tourist
12 0 0

Hi again,

 

After applying the fixes mentioned above, I didn't get errors for a while.

 

Unfortunately, I now ran into a new RuntimeError:

undefined method 'map' for nil on line 7

 

Is this something you are familiar with?

 

runtimeerror.png

Thanks

0 Likes
SBD_
Shopify Staff
Shopify Staff
1081 146 193

Hey @Dominic_Nadeau

 

Output.shipping_rates always need to be set. In this case it's only set when address1 has a value. A quick fix would be to replace the last end with:

 

else
  Output.shipping_rates = Input.shipping_rates
end

Be sure to test the update before publishing :)

0 Likes
Dominic_Nadeau
Tourist
12 0 0

Hi Scotty,

 

It seemed like the script was working fine for a while. We then received a few messages from customers trying to order from Hawaii that would not see any rate returned.

Deactivating the script enabled the "Standard Shipping" rate to be shown. Would you have any idea why this would happen and how to fix it?

 

 Here's the complete script:

#Exclude Expedited option when PO Box is present
unless Input.cart.shipping_address.address1.nil?
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']
else
hideship = ['Standard Shipping']
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

else
Output.shipping_rates = Input.shipping_rates
end

0 Likes
Dominic_Nadeau
Tourist
12 0 0

Hi Scotty,

 

One thing I forgot to mention: Hawaii is in a different shipping profile which does not return Expedited Shipping rates. I think that since there is no PO Box in the address, it applies the "Else" condition, hiding the Standard shipping rate. How can I only activate the script for one shipping profile?

 

Thank you

0 Likes
SBD_
Shopify Staff
Shopify Staff
1081 146 193

Hey @Dominic_Nadeau 

Scripts can't see shipping profiles but you could examine the shipping address instead. Maybe something like:

unless Input.cart.shipping_address.address1.nil? or Input.cart.shipping_address.province_code == 'HI'
  ...

 
Be sure to test this as a preview before publishing.

0 Likes
Dominic_Nadeau
Tourist
12 0 0

Hey @SBD_

Thanks for your answer.

 

If I understand correctly, the complete code should look like this:

#Exclude Expedited option when PO Box is present
unless Input.cart.shipping_address.address1.nil?
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']
else
unless Input.cart.shipping_address.province_code == 'HI'
hideship = ['Standard Shipping']
end
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

else
Output.shipping_rates = Input.shipping_rates
end

 

If I want to add province codes, should it look like 'HI, AK, AE' ?

I am also getting an "undefined method 'address1' for nil  error on line 2 since before I added the new code...

 

I am going to test things out, but I feel like the error will get in the way. 

0 Likes
Dominic_Nadeau
Tourist
12 0 0

Update*

Hey @SBD_,

I managed to get the script working again (Standard shipping is hidden except for Hawaii, ALaska and the Armed Forces).

One functionality I lost in the meantime is to hide Expedited shipping and show Standard Shipping when any version of the word PO Box is in the address1 field.

 

Could you please help me figure out what the issue could be? Complete code:

 

#Exclude Expedited option when PO Box is present
unless Input.cart.shipping_address.address1.nil? or Input.cart.shipping_address.province_code == 'HI' or Input.cart.shipping_address.province_code == 'AK' or Input.cart.shipping_address.province_code == 'AA' or Input.cart.shipping_address.province_code == 'AE' or Input.cart.shipping_address.province_code == 'AP'
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']
else

hideship = ['Standard Shipping']

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
else
Output.shipping_rates = Input.shipping_rates
end

 

Thanks for your help!

0 Likes