less than a minute read • Updated 9 minutes ago
Automatically select the first shipping rate
How to have the cart and checkout auto select the first shipping rate returned for the customer's address.
The following steps allow you to have the cart/checkout automatically select the first shipping rate from the options returned after a customer enters their shipping address.
{% if context == "cart" or context == "checkout" %}
<script>
FC.client.on(FC.json.context + '-shipping-options-update.done', autoSelectRate);
function autoSelectRate(params) {
var prefix = (params.address.prefix == 'shipping') ? '' : params.address.prefix + "_";
if (jQuery('[name=' + prefix + 'shipping_service_id]:first').length) {
jQuery('[name=' + prefix + 'shipping_service_id]:first').trigger('click');
FC.Template(FC.json.context).clearOutput();
if (FC.json.context == 'cart' && FC.json.shipping_address.shipping_results.length > 1) {
FC.cart.showShippingLocationInput();
} else if (FC.json.context == "checkout") {
FC.checkout.renderShippingRates(params.address);
}
}
}
</script>
{% endif %}
Copy the code you see to your computer's clipboard.
In the Foxy admin (https://admin.foxy.io), go to Settings > Templates.
Paste the code into the Custom footer field.
Scroll down until you see the "Update" button and click to save.
Run a test by adding some of your shippable products to the cart and enter a shipping address to ensure that it works as expected.