Remote-area surcharge by postal-code prefixes

Created by Lisa Erkens, Modified on Mon, 13 Oct at 4:48 PM by Lisa Erkens

{% liquid
  # Add a surcharge if the destination postal code starts with any of the configured prefixes.

  # --- editable settings ---
  assign service_name = "Remote Area Shipping"
  assign service_code = "REMOTE_AREA"
  assign currency = "USD"
  assign description = "Base + remote area surcharge if applicable"
  assign base_price = 1200                      # cents
  assign remote_surcharge = 800                 # cents
  assign postal_prefixes_raw = "99950|99951|H0H"  # pipe-separated; case-insensitive

  # --- logic ---
  assign postal = shopify_rate_check.destination.postal_code | upcase
  assign prefixes = postal_prefixes_raw | upcase | split: "|"
  assign is_remote = false

  for p in prefixes
    assign p_len = p | size
    assign test = postal | slice: 0, p_len
    if test == p and p_len > 0
      assign is_remote = true
      break
    endif
  endfor

  assign shipping_price = base_price
  if is_remote
    assign shipping_price = shipping_price | plus: remote_surcharge
  endif

  assign rate = rate | merge: "currency", currency
  assign rate = rate | merge: "service_name", service_name
  assign rate = rate | merge: "service_code", service_code
  assign rate = rate | merge: "total_price", shipping_price
  assign rate = rate | merge: "description", description

  assign rates = rates | array_append: rate
%}
{
  "rates": {{ rates | array_wrap | json }}
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article