Personalized checkout and custom promotions with Shopify Scripts
Hi there,
I want to show/hide a few shipping methods based on the ZIP code entered at the checkout. I am trying to write a shipping script for that but facing an issue that I can't check if a Zipcode starts with some value or not. So, for example, if a customer enters the ZIP code "BT1 1BG" I want to check if it starts with "BT" or not.
According to my knowledge, I can only check the exact match or partial match but in case of the partial match it would return true if "BT" exists anywhere in the ZIP code, for example, "EC1A 1BT"; and I don't want this result. I tried to use "start_with" method, but it is not working in the Shipping script.
Any solution or workaround would be much appreciated!
Thanks!
Solved! Go to the solution
This is an accepted solution.
Hi @m_waqas,
I am wondering if you just forgot the "?" (question mark) at the end of the start_with? method. Because when you generate a sample shipping script for hiding methods, it actually uses the start_with? method:
Given that, here is the script that seemed to work for me. I also added a hash in the beginning to make it easier to add new use cases.
# make a mapping of which shipping methods to remove based on zip prefixes
mapping = {
'BT' => 'USPS',
'16' => 'FEDEX_EXPRESS'
}
# empty array where we will store our removed zip codes
removed_zips = []
# store the customer's zip code
customer_zip = Input.cart.shipping_address.zip
# step through our map and test each one, add to our list of removals if matching
mapping.each do |set|
removed_zips << set[1] if (customer_zip.upcase.start_with? set[0])
end
# use our removal list while stepping though passed in shipping rates
Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
removed_zips.each do |zip|
shipping_rate.name.upcase.start_with?(zip)
end
end
If that worked for you, feel free to mark the solution as accepted.
Thanks!
Matthew
This is an accepted solution.
Hi @m_waqas,
I am wondering if you just forgot the "?" (question mark) at the end of the start_with? method. Because when you generate a sample shipping script for hiding methods, it actually uses the start_with? method:
Given that, here is the script that seemed to work for me. I also added a hash in the beginning to make it easier to add new use cases.
# make a mapping of which shipping methods to remove based on zip prefixes
mapping = {
'BT' => 'USPS',
'16' => 'FEDEX_EXPRESS'
}
# empty array where we will store our removed zip codes
removed_zips = []
# store the customer's zip code
customer_zip = Input.cart.shipping_address.zip
# step through our map and test each one, add to our list of removals if matching
mapping.each do |set|
removed_zips << set[1] if (customer_zip.upcase.start_with? set[0])
end
# use our removal list while stepping though passed in shipping rates
Output.shipping_rates = Input.shipping_rates.delete_if do |shipping_rate|
removed_zips.each do |zip|
shipping_rate.name.upcase.start_with?(zip)
end
end
If that worked for you, feel free to mark the solution as accepted.
Thanks!
Matthew
I'm working with the script example from Shopify and ran into the same problem. The solution for me was much simpler. Just replace include? with start_with?.
class ZipCodeSelector
def initialize(match_type, zip_codes)
@comparator = match_type == :exact ? '==' : 'include?'
@Zip_codes = zip_codes.map { |zip_code| zip_code.upcase.strip }
end
def match?(zip_code)
@Zip_codes.any? { |zip| zip_code.to_s.upcase.strip.send(@comparator, zip) }
end
end
class ZipCodeSelector
def initialize(match_type, zip_codes)
@comparator = match_type == :exact ? '==' : 'start_with?'
@Zip_codes = zip_codes.map { |zip_code| zip_code.upcase.strip }
end
def match?(zip_code)
@Zip_codes.any? { |zip| zip_code.to_s.upcase.strip.send(@comparator, zip) }
end
end
Hope this helps anyone looking for a quick solution.
Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024The Hydrogen Visual Editor is now available to merchants in Shopify Editions | Summer '...
By JasonH Sep 2, 2024Note: Customizing your CSS requires some familiarity with CSS and HTML. Before you cust...
By JasonH Aug 12, 2024