Personalized checkout and custom promotions with Shopify Scripts
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-ra...).
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:
/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/
./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.
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025