We applied shipping rate to grand total but not display directly on cart page.
So, we need to reload the page and then after the display that shipping rate.
Now, We need to load or update cart total table on cart page of Magento using ajax.
Hi @Ashish_k_php,
You won't be able to do that without customization or a 3rd-party module.
Maybe you can satrt looking at the marketplace: https://marketplace.magento.com/catalogsearch/result/?q=compare#q=ajax&idx=m2_cloud_prod_default_pro...
public function cart_totalAction()
{
$layout = $this->getLayout();
$totalsBlock = $layout->createBlock('checkout/cart_totals')->setTemplate('checkout/cart/totals.phtml');
return $totalsBlock->toHtml();
}
This returns the NULL result.
Please Help.
Thanks.
I was stuck in a similar situation. I had to change shipping rate and so the total on cart page according to the selected country using ajax.
Create your ajax function
jQuery('#country').change(function(){
var countryId = (jQuery(this).val());
jQuery('.totals-inner-top').hide();
jQuery('#loadderimage').show();
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('#loadderimage').hide();
jQuery('#shopping-cart-totals-table').replaceWith(data.refreshtotalBLK);
jQuery('.totals-inner-top').show();
}
else
{
if(data.status == 0)
{
alert(data.error);
}
}
}
});
});
Now this is the function
public function custom_update_countryAction()
{
try {
$country_id = $this->getRequest()->getParam('country_id');
$code = 'tablerate_bestway';
$this->_getQuote()->getShippingAddress()
->setCountryId($country_id)
->setShippingMethod($code)
->setCollectShippingRates(true)
->collectTotals()
->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));
}
This is how I did it on magento 1.9.2
Updating cart totals based on Delivery types selected.
I have custom plugin written to change the shipping using ajax call as well.
var checktotal = $$('div.cart-totals').first();
checktotal.update('<div class="loading-ajax"> </div>');
var delchgs = $$('.delcharges').first();
delchgs.update('€'+ actualship);
var url = '/checkout/cart/updatetotal/';
var pars = '';
pars += 'del_price=' + shipping ;
new Ajax.Request(url, {
method: 'post',
parameters: pars,
onSuccess: function (transport) {
if (transport) {
checktotal.update(transport.responseText);
//alert(transport.responseText);
return true;
}
else {
//tableResult.update('**bleep**! You are beyond #10...').setStyle({ background: '#fdd' });
alert("Ajax no return");
}
}
});
This is updatetotal method. you can use this code in your plugin by extending cartcontroller class.
public function updatetotalAction()
{
//$this->loadLayout();
$layout=$this->getLayout();
$delPrice = $this->getRequest()->getPost('del_price');
$cart = $this->_getCart();
$quote = $cart->getQuote();
$store = Mage::app()->getStore($quote->getStoreId());
$carriers = Mage::getStoreConfig('carriers', $store);
foreach ($carriers as $carrierCode => $carrierConfig) {
Mage::log('Handling Fee(Before):' . $store->getConfig("carriers/{$carrierCode}/handling_fee"), null, 'shipping-price.log');
$store->setConfig("carriers/{$carrierCode}/handling_type", 'F'); #F - Fixed, P - Percentage
$setNewPrice = Mage::getSingleton('core/session')->getDelPrice();
$store->setConfig("carriers/{$carrierCode}/handling_fee", $delPrice);
$setNewTitle = Mage::getSingleton('core/session')->getDelTitle();
$store->setConfig("carriers/{$carrierCode}/name", $setNewTitle);
###If you want to set the price instead of handling fee you can simply use as:
#$store->setConfig("carriers/{$carrierCode}/price", $newPrice);
$cart->save();
Mage::log('Handling Fee(After):' . $store->getConfig("carriers/{$carrierCode}/handling_fee"), null, 'shipping-price.log');
}
$quoteData= $quote->getData();
$grandTotal=$quoteData['grand_total'];
//$totalsBlock = $layout->createBlock('checkout/cart_totals')->setTemplate('checkout/cart/totals.phtml');
$totalsBlock.= '<table>';
$totalsBlock .= $layout->createBlock('checkout/cart_totals')->renderTotals();
$totalsBlock.= '<tr><td>Grand Total</td><td>'.Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol().number_format($grandTotal,2).'</td>';
$totalsBlock.= '<table>';
echo $totalsBlock ;
}