Autoselect default shipping option

In using custom code form this page:
https://wiki.foxycart.com/v/2.0/shipping/custom_code

I have:
rates.add(10001, 8, 'USPS', 'Priority');
if (cart['_embedded']['fx:shipment']['total_item_price'] >= 75) {
rates.filter(10001).price(0).service('Free USPS Priority Shipping');
}


This works properly, but the user has to select the shipping option even though there is only one. Some of the older code variants had an autoselect. How can I set this up where this shipping option is autoselected so they do not have to? Thanks!
Tagged:
Comments
  • fc_marijafc_marija FoxyCart Team
    Hi @yyhmsg -

    You can put a bit of code on the "custom footer" in the configuration page to auto-select the first rate (and in this case, the only rate). Copy the code in the that follows to your computer's clipboard and paste it into the "custom footer" text area in the templates configuration page (you may need to select "add custom header and footer code to your templates" to get it to appear).
    {% if context == "cart" or context == "checkout" %}
    <script>
    FC.client.on("cart-shipping-options-update.done", autoSelectRate);
    FC.client.on("checkout-shipping-options-update.done", autoSelectRate);

    function autoSelectRate(params) {
    jQuery("[name=shipping_service_id]:first").trigger("click");
    FC.Template(FC.json.context).clearOutput();
    if (FC.json.context == "cart") {
    FC.cart.showShippingLocationInput();
    } else {
    FC.checkout.renderShippingRates(params.address);
    }
    }
    </script>
    {% endif %}
    Let us know if you need more detailed instructions for adding that snippet.
  • Awesome, works perfectly! Thanks!
  • Awesome thanks!
Sign In or Register to comment.