1

I have created a country dropdown on cart page with the following code.

 <div class="country-select-box">
    <label for="country" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
        <div class="input-box">
            <?php echo Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect($countryId) ?>
        </div>
</div>

The variable that is passed to getCountryHtmlSelect function i.e $countryId, I get the value of this variable through this code

<?php
    $checkout = Mage::getSingleton('checkout/session')->getQuote();
    $countryId = $checkout->getBillingAddress()->getCountryId();
?>

This code is also on the same page i.e cart.phtml

Now on country select I am making a ajax call. Here is the code

jQuery('#country').change(function(){
var countryId = (jQuery(this).val());
//jQuery('.totals-inner-top').html('<div class="loader-image" id="loadderimage"><img src="/media/wysiwyg/loader-totals.gif" /></div>');
jQuery.ajax({
    url: "<?php echo $this->getUrl('checkout/cart/custom_update_country')?>",
    dataType : 'json',
    type : 'POST',
    data: {'country_id': countryId},
    success : function(data)
    {
        if(data.status == 1)
        {
            //jQuery('.totals-inner-top').html(data.refreshtotalBLK);
            location.reload();
        }
        else
        {   
            if(data.status == 0)
            {
                alert(data.error);
            }
        }
    }
    });

});

Now my custom function that is hit on ajax call.It is in CartController.php

public function custom_update_countryAction()
{
    try {
        $country_id = $this->getRequest()->getParam('country_id');
        $code = 'tablerate_bestway';

        $address = $this->_getQuote()->getBillingAddress()->setCountryId($country_id);

        $this->_getQuote()->getShippingAddress()
            ->setCountryId($country_id)
            ->setShippingMethod($code)
            ->setCollectShippingRates(true)
            ->collectTotals();

        $this->_getQuote()->save();
        $this->_getSession()->setCartWasUpdated(true);

        $response['status'] = 1;        
        //$html = $this->getLayout()->createBlock('checkout/cart_totals')->setTemplate('checkout/cart/totals.phtml')->toHtml();
        //$response['refreshtotalBLK'] = $html;         
    } catch (Exception $e) {
            $response['status'] = 0;
            $response['error'] = $this->__('Can not save country.');
    }
    return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));

}

Explainaation of the function. I am getting country code in variable $country_id and setting it as country for billing address and shipping address and the value of $code='tablerate_bestway' ,I am setting it as shipping method. From magento admin panel I have enabled table rate shipping method and exported my tablerates.csv with appropriate content. Now when country is selected from dropdown then the function runs and everything works as expected. A screenshot of totals section on cart page enter image description here Delivery gets changed according to tablerates.csv Everything works fine. When I try to do the same thing without page reload then it doesn't work as expected. Delivery gets updated but totals keep on increasing with respect to previous total. I can't understand why this happening. For code to change total without page reload just uncomment the commented lines.And you have to comment location.reload(). If anyone have any idea then please help

Please check the screenshot. Total becomes 90 after 4 to 5 ajax call. It keeps increasing on every ajax call with respect to previous total. enter image description here

2
  • try console.log(data.refreshtotalBLK); in your js, does the total in developer console of your browser get incremented each time still? Commented Feb 13, 2018 at 11:48
  • 1
    Yes in console also the value is getting incremented each time. Commented Feb 13, 2018 at 12:00

1 Answer 1

1

After saving quote

$this->_getQuote()->save();

Try to recollect quote totals by running this:

$item->getQuote()->setTotalsCollectedFlag(false)->collectTotals();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.