Auto select code not working when custom shipping endpoint rates->error used

SimonSimon Member
Hi,

I'm using the following snippet recently posted here to auto select the first shipping rate when using a custom shipping endpoint.

<script>FC.client.on("cart-shipping-options-update.done", autoSelectRate);
FC.client.on("checkout-shipping-options-update.done", autoSelectRate);

function autoSelectRate() {
jQuery("[name='shipping_service_id']:first").attr("checked", "checked").trigger("click");
if (FC.json.context == "cart") {
FC.Template(FC.json.context).clearOutput();
FC.cart.showShippingLocationInput();
}
}</script>


It works great until I use the $rates->error("....."); function for orders we cannot provide a rate for. As there is no rate to auto select it hangs and slowly dies a death.

How do I modify the snippet to only fire when there is a rate available?


Tagged:
Comments
  • fc_adamfc_adam FoxyCart Team
    @Simon,

    Oh sorry about that! I can see that error on the cart.

    The following should work for it:
    <script>FC.client.on("cart-shipping-options-update.done", autoSelectRate);
    FC.client.on("checkout-shipping-options-update.done", autoSelectRate);

    function autoSelectRate() {
    jQuery("[name='shipping_service_id']:first").attr("checked", "checked").trigger("click");
    if (FC.json.context == "cart" && FC.json.shipping_address.shipping_results.length > 0) {
    FC.Template(FC.json.context).clearOutput();
    FC.cart.showShippingLocationInput();
    }
    }</script>
  • Thanks Adam, works as expected.
  • @fc_adam

    I was a bit quick in saying it was all working as expected.

    If I add an item to the cart and do not calculate delivery, when I proceed to the checkout and fill out the delivery address the first rate is not auto selected.

    This happens if I manually fill out the zip code or if it is selected with autofill.

    If I then overwrite the zip code or just re-enter the last character again the first rate available is auto selected correctly. It's almost as if the function is just not running on the first form fill.

    Any ideas what could be causing this?

    Cheers,
    Simon
  • fc_adamfc_adam FoxyCart Team
    @Simon,

    Ah good catch, sorry about that. Give this one a try:
    <script>FC.client.on("cart-shipping-options-update.done", autoSelectRate);
    FC.client.on("checkout-shipping-options-update.done", autoSelectRate);

    function autoSelectRate() {
    setTimeout(function() {
    jQuery("[name='shipping_service_id']:first").attr("checked", "checked").trigger("click");
    if (FC.json.context == "cart" && FC.json.shipping_address.shipping_results.length > 0) {
    FC.Template(FC.json.context).clearOutput();
    FC.cart.showShippingLocationInput();
    }
    }, 10);
    }</script>
  • @fc_adam

    That is working now, thank you.
Sign In or Register to comment.