Reputation: 955
I'm trying to show the different rates depending on postcode in the shopify APP. I am currently using the script that they supply while also trying to tweak it to make it work for me. At the moment it isn't showing any post code because its meeting all the conditions.
If any one could help that would be brilliant.
SHOW_RATES_FOR_ZIP_PROVINCE_COUNTRY = [
{
country_code: "UK",
zip_code_match_type: :exact,
zip_codes: ["BR"],
rate_match_type: :exact,
rate_names: ["Zone 1"],
},
]
SHOW_RATES_FOR_ZIP_PROVINCE_COUNTRY_TWO = [
{
country_code: "UK",
zip_code_match_type: :exact,
zip_codes: ["DA"],
rate_match_type: :exact,
rate_names: ["Zone 2"],
},
]
SHOW_RATES_FOR_ZIP_PROVINCE_COUNTRY_THREE = [
{
country_code: "UK",
zip_code_match_type: :exact,
zip_codes: ["TN"],
rate_match_type: :exact,
rate_names: ["Zone 3"],
},
]
# ================================ 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
# ================================================================
# ShowRatesForZipProvinceCountryCampaign
#
# If the cart's shipping address zip/province/country match the
# entered settings, the entered rate(s) are shown, and all other
# rates are hidden. Otherwise, the entered rate(s) are hidden.
# ================================================================
class ShowRatesForZipProvinceCountryCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart, shipping_rates)
address = cart.shipping_address
@campaigns.each do |campaign|
zip_code_selector = ZipCodeSelector.new(campaign[:zip_code_match_type], campaign[:zip_codes])
rate_name_selector = RateNameSelector.new(campaign[:rate_match_type], campaign[:rate_names])
if address.nil?
full_match = false
else
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)
full_match = country_match && province_match && zip_match
end
shipping_rates.delete_if do |shipping_rate|
rate_name_selector.match?(shipping_rate) != full_match
end
end
end
end
CAMPAIGNS = [
ShowRatesForZipProvinceCountryCampaign.new(SHOW_RATES_FOR_ZIP_PROVINCE_COUNTRY),
ShowRatesForZipProvinceCountryCampaign.new(SHOW_RATES_FOR_ZIP_PROVINCE_COUNTRY_THREE),
ShowRatesForZipProvinceCountryCampaign.new(SHOW_RATES_FOR_ZIP_PROVINCE_COUNTRY_TWO),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart, Input.shipping_rates)
end
Output.shipping_rates = Input.shipping_rates
Upvotes: 0
Views: 264
Reputation: 11427
You need to be more specific. What is your expected result for a sample code A. What is your expected result for a sample code B. And on... and on. Those scripts work awesome, but without knowing what you are looking for, I doubt anyone will provide you any answers of much value.
Upvotes: 1