Using Javascript to Trigger a Recalc in the Cart

Forum Forums Tutorials Using Javascript to Trigger a Recalc in the Cart

This topic contains 0 replies, has 1 voice, and was last updated by  Anonymous 14 years, 1 month ago.

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #48724

    Anonymous
    Participant

    If you allow customers to change ordering options in the shopping cart, any difference in price that results from changing options will not be reflected in the shopping cart totals until they click “recalculate”.

    It is possible to force the cart to recalculate using Javascript; however, calling this javascript function will force the page to reload. If you have a substantial number of ordering options, forcing a recalc may interfere substantially with the customer’s ability to use the interface since it would reload each time an option is changed.

    You will have to gauge whether the benefits of displaying the correct values without the user selecting “Recalculate” outweigh the risks of reloading the page and disrupting the customer interface.

    To force a recalc in the cart each time an ordering option is changed, enter the following code in the bottom of the “Text at the bottom of the Shopping Cart screen:” box in Commerce Setup->Order System->Shopping Cart:

     

    <script type="text/javascript"> 
     
     function forceRecalc(){ 
        var func; 
        func = document.getElementsByName("function"); 
        for (i = 0; i < func.length; i++) { 
            if (func[i].type == "hidden") { 
                zip_recalc = 1; 
                func[i].value = "zip_code_changed"; 
                window.document.order.submit(); 
            } 
        } 
     } 
     
     var options = document.getElementsByTagName("select"); 
     
     for(n=0; n<options.length; n++){ 
        if(options[n].className == 'cart' || options[n].className == 'taxnship'){ 
            options[n].onchange = function(){ 
                forceRecalc(); 
            } 
        } 
     } 
     
     </script> 
    

     

    If you would like to update the shopping cart totals automatically when the shipping options are changed as well, you can use the following script:

     

    <script type="text/javascript"> 
     
     function forceRecalc(){ 
        var func; 
        func = document.getElementsByName("function"); 
        for (i = 0; i < func.length; i++) { 
            if (func[i].type == "hidden") { 
                zip_recalc = 1; 
                func[i].value = "zip_code_changed"; 
                window.document.order.submit(); 
            } 
        } 
     } 
     
     var options = document.getElementsByTagName("select"); 
     
     for(n=0; n<options.length; n++){ 
        if(options[n].className == 'cart' || options[n].className == 'taxnship' || options[n].className=='ship'){ 
            options[n].onchange = function(){ 
                forceRecalc(); 
            } 
        } 
     } 
     
     var input_field = document.getElementsByTagName("input"); 
     
     for(i=0; i<input_field.length; i++){ 
         if(input_field[i].type=='radio' && input_field[i].name=='shipping'){ 
            input_field[i].onchange = function(){ 
                forceRecalc(); 
            } 
        } 
     } 
     
     </script>
    

     

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.