Hide rates for a specific zip code, province, and country

aaqibalighuman
Shopify Partner
8 0 0

Hello, Can Someone please help me to resolve the error?

 

here is the link from where I am copying the script.

 

https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/shipping-scripts#hide-ra...

 

that is the script I am adding.

 

# ================================ Customizable Settings ================================
# ================================================================
# Hide Rate(s) for Zip/Province/Country
#
# If the cart's shipping address country/province/zip match the
# entered settings, the entered rate(s) are hidden.
#
#   - 'country_code' is a 2-character abbreviation for the
#     applicable country or region
#   - 'province_code' is a list of 2-character abbreviations for
#     the applicable provinces or states
#   - 'zip_code_match_type' determines whether the below strings
#     should be an exact or partial match. Can be:
#       - ':exact' for an exact match
#       - ':partial' for a partial match
#   - 'zip_codes' is a list of strings to identify zip codes
#   - 'rate_match_type' determines whether the below strings
#     should be an exact or partial match. Can be:
#       - ':exact' for an exact match
#       - ':partial' for a partial match
#       - ':all' for all rates
#   - 'rate_names' is a list of strings to identify rates
#     - if using ':all' above, this can be set to 'nil'
# ================================================================
HIDE_RATES_FOR_ZIP_PROVINCE_COUNTRY = [
  {
    country_code: "US",
    province_code: "CA",
    zip_code_match_type: :exact,
    zip_codes: ["90210"],
    rate_match_type: :exact,
    rate_names: ["Shipping Rate"],
  },
]

# ================================ Script Code (do not edit) ================================
# ================================================================
# ZipCodeSelector
#
# Finds whether the supplied zip code matches any of the entered
# strings.
# ================================================================
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

# ================================================================
# RateNameSelector
#
# Finds whether the supplied rate name matches any of the entered
# names.
# ================================================================
class RateNameSelector
  def initialize(match_type, rate_names)
    @match_type = match_type
    @comparator = match_type == :exact ? '==' : 'include?'
    @rate_names = rate_names&.map { |rate_name| rate_name.downcase.strip }
  end

  def match?(shipping_rate)
    if @match_type == :all
      true
    else
      @rate_names.any? { |name| shipping_rate.name.downcase.send(@comparator, name) }
    end
  end
end

# ================================================================
# HideRatesForZipProvinceCountryCampaign
#
# If the cart's shipping address zip/province/country match the
# entered settings, the entered rate(s) are hidden.
# ================================================================
class HideRatesForZipProvinceCountryCampaign
  def initialize(campaigns)
    @campaigns = campaigns
  end

  def run(cart, shipping_rates)
    address = cart.shipping_address

    return if address.nil?

    @campaigns.each do |campaign|
      zip_code_selector = ZipCodeSelector.new(campaign[:zip_code_match_type], campaign[:zip_codes])

      country_match =  address.country_code.upcase.strip == campaign[:country_code].upcase.strip
      province_match = address.province_code.upcase.strip == campaign[:province_code].upcase.strip
      zip_match = zip_code_selector.match?(address.zip)

      next unless country_match && province_match && zip_match

      rate_name_selector = RateNameSelector.new(campaign[:rate_match_type], campaign[:rate_names])

      shipping_rates.delete_if do |shipping_rate|
        rate_name_selector.match?(shipping_rate)
      end
    end
  end
end

CAMPAIGNS = [
  HideRatesForZipProvinceCountryCampaign.new(HIDE_RATES_FOR_ZIP_PROVINCE_COUNTRY),
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart, Input.shipping_rates)
end

Output.shipping_rates = Input.shipping_rates






I have follow the step by step instruction

I am attaching screen shot below that is showing error.

Screenshot (79).png


Reply 1 (1)
PaulNewton
Shopify Partner
4765 391 1021

Hello @aaqibalighuman ,

Not exactly sure what's going on there I'm not sure of how to reproduce that error.

Checking your code against what's on the docs there are no diffs.

And I get no errors when trying the script.

 

If that is the unmodified script supplied by the shopify help docs it may be to do the stores region and subsequent address fields.

From the note immediately before that script:

Note

The use of zip_code, province and province_code, and country and country_code is specific to the reg...

So for example if in the United Kingdoms I suspect you may need to use postal_code instead of zip_code.

 

 

If that's the case no idea why they'd make that note,  and not try to make the script resilient or provide regional variants.

 

If you still cannot solve it I'd try first try doing a test checkout to see if the script is in fact working, and clear your browser cache and try again.

 

If still not resolved I'd the Plus stores MSM.

 

 

Similar error posts: https://community.shopify.com/c/forums/searchpage/tab/message?advanced=false&allow_punctuation=false... 

Finally Combinable Discounts - EARLY ACCESS ONLY - Use Free Shipping With Other Discounts

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org
Problem Solved? ✔Accept and Like solutions to help future merchants
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com