Hi. I’m looking to make a very simple alteration to the example script ‘Show rates by customer tag’ (https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/shipping-scripts#show-rates-by-customer-tag).
The change is that I don’t want all other rates deleting if there is a match, but the specified rate should only show if there is a match. I would have thought the adapted code below would do what I’m looking for, but it’s not working for me:
next unless customer_tag_selector.match?(cart.customer)
shipping_rates.delete_if do |shipping_rate|
rate_name_selector.match?(shipping_rate)
end
To modify the example script “Show rates by customer tag” so that only the specified rate shows if there is a customer tag match, you can make the following changes to the code:
def select_rates(rates, cart)
customer_tag_selector = /your_customer_tag/ # Replace 'your_customer_tag' with the specific customer tag you want to match
rate_name_selector = /your_rate_name/ # Replace 'your_rate_name' with the name of the rate you want to match
matching_rates = rates.select do |rate|
next unless rate.available_to_customer?
# Keep the rate if it matches the customer tag and rate name
rate_name_selector.match?(rate.name.downcase) && customer_tag_selector.match?(cart.customer)
end
matching_rates.empty? ? rates : matching_rates
end
Here’s what you need to do:
- Replace
/your_customer_tag/ with the specific customer tag you want to match. For example, if your customer tag is “VIP”, you would change it to /VIP/.
- Replace
/your_rate_name/ with the name of the rate you want to match. For example, if your rate name is “Express Shipping”, you would change it to /Express Shipping/i (the i is added to make the match case-insensitive).
Hi and thanks for your input on this. However, I’m not looking to ‘only the specified rate shows if there is a customer tag match’ (that’s what the example code already does).
I instead simply want that rate hiding in all circumstances unless there’s a customer match and then it should be shown, along with the other rate options.
I also don’t think I should need to hard code in the customer tag and rate name given those variables have already been specified elsewhere in the script.