PO BOX checker script production error

Solved
Highlighted
Tourist
12 0 0

Hi,

 

I managed to get a script to work to hide Expedited Shipping for PO Boxes and show Standard Shipping instead (It's the only time we show Standard Shipping).

 

We launched a week ago and since then, I got 8 production errors (with dozens of sales). It's always the same RuntimeError: undefined method 'upcase' for nil


Can anyone help me figure out what's going on? I really appreciate it!

 

Here's the script, the error happens on row 2 where I put the word in bold:

 

#Script begin

#Exclude Expedited option 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']
else
hideship = ['Standard']
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

#Script end

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 170

Hey @Dominic_Nadeau

 

This will occur if address1 is blank.

 

Wrap your login in "if there's a shipping address1...", or use a Safe Navigation Operator.

Notice; Out of office, replies will be delayed until my return. Thanks!
Highlighted
Tourist
12 0 0

Ok, so what I understand is that it does not really affect the customer's experience since the error only triggers when a required field is blank (it can't be blank anyway to proceed to shipping). So, that's great, I'm worrying less.

 

Could you please elaborate on how to implement the IF within my code? I am not really knowledgeable on Ruby.

 

Thank you

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 170

Are you able to share the store URL? The scripts editor should shed some light on how these are coming through.

Notice; Out of office, replies will be delayed until my return. Thanks!
0 Likes
Highlighted
Tourist
12 0 0
Yes, the store URL is ironbullstrength.com
0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 170

Thanks @Dominic_Nadeau 

 

it can't be blank anyway to proceed to shipping

I thought the same thing, so I just dug a little deeper. It turns out Google Pay can trigger Shipping Scripts without an address1. The timing seems to line up with the latest error that was generated + order #8643 (which used GPay).

 

To get around this, short-circuit the script:

 

return if cart.shipping_address.address1.nil?

...the rest of your script

 

Notice; Out of office, replies will be delayed until my return. Thanks!
0 Likes
Highlighted
Tourist
12 0 0
Hi,

Thank you for your reply.

I tried including this at the beginning of the script.

This generated the following error code on its line:
Undefined method 'cart' for main

What does that mean?


0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 170

Apologies, typo:

 

return if Input.cart.shipping_address.address1.nil?

Be sure to test this before publishing. 

Notice; Out of office, replies will be delayed until my return. Thanks!
0 Likes
Highlighted
Tourist
12 0 0

Hi Scotty,

 

Yesterday, I added the line to the script and tested it out without errors before publishing.

 

Today, though, I received 2 new RuntimeError for "unexpected return".

What does that mean?

 

It was followed by orders US#8679 and US#8680 both paid using Shopify Payment.

 

Thank you for you help

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 170

This is an accepted solution.

Hey @Dominic_Nadeau 

 

Try wrapping the code instead:

 

unless Input.cart.shipping_address.address1.nil?
  ...your code
end
Notice; Out of office, replies will be delayed until my return. Thanks!