Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Update Cart Content with AJAX
 
Luiz Alvaro
Jr. Member
 
Total Posts:  11
Joined:  2010-12-29
 

Hello Folks,

I Trying to update .data-table table with ajax after delete some item from magento cart, I couldn’t figure out who put this to work.

Delete function

public function deleteAction(){
        $id 
= (int) $this->getRequest()->getParam('id');
        if (
$id{
            
try {
                $this
->_getCart()->removeItem($id)
                  ->
save();
                
$this->_getSession()->setCartWasUpdated(true);
                if (!
$this->_getCart()->getQuote()->getHasError()){
                    
                    $response[
'status''SUCCESS';
                    
$response['message'$message;
                    
$this->loadLayout();
            
$block $this->getLayout()->getBlock('checkout.cart');
                    
Mage::register('referrer_url'$this->_getRefererUrl());
                    
$checkout $block->toHtml();
                                      
                    
$response['checkout'$checkout;
                                    
                
}
                
                $this
->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
            return;
            
catch (Exception $e{
                $this
->_getSession()->addError($this->__('Cannot remove the item.'));
                
Mage::logException($e);
            
}
        }
        
        }

default.phtml button/script

<script type="text/javascript">
    function 
setLocationAjax(url,id){
            url 
+= '&isAjax;=1';
            
url url.replace("checkout/cart","ajax/index");
            try 
{
                jQuery
.ajax{
                    url 
url,
                    
dataType 'json',
                    
success : function(data{
                        
if(jQuery('.data-table')){
                            jQuery
('.data-table').replaceWith(data.checkout);
                        
}
                            
                                     
                    }
                }
);
            
catch (e{
            }
        }
        
    </script>
    

    
<td class="a-center"><a href="[removed]void(0)" onclick="setLocationAjax('<?php echo $this->getDeleteUrl(); ?>','<?php echo $_item->getId()?>')" title="<?php echo $this->__('Remove item')?>" class="btn-remove btn-remove2"><?php echo $this->__('Remove item')?></a></td>

sry about my english >.<

 
Magento Community Magento Community
Magento Community
Magento Community
 
bhavikgandhi
Jr. Member
 
Total Posts:  3
Joined:  2013-07-09
 

Hii thnx for the code . i used it and make some changes and it work....

(if you installed vs ajax add to cart magento extension)

In app/design/frontend/default/default/template/ajax/checkout/cart/sidebar/default.phtml, put below code

<button type="button" title="<?php echo $this->__('Remove From Cart') ?>” class="btn-remove btn-remove2” onclick="setLocationdelAjax(’<?php echo $this->getDeleteUrl(); ?>’,’<?php echo $_item->getId()?>’)">x</button>

function setLocationdelAjax(url,id){

//alert(url);
url += ‘&isAjax;=1’;
url = url.replace("checkout/cart”,"ajax/index");
try {
jQuery.ajax( {
url : url,
dataType : ‘json’,
success : function(data) {
jQuery(’#sidecart’).html(data.checkout);
jQuery(’.cart-text’).html(data.total);

}
});
} catch (e) {
}
}

and in app/code/community/vs/ajax/controllers//indexController.php

public function deleteAction(){
$id = (int) $this->getRequest()->getParam(’id’);
if ($id) {
try {
$this->_getCart()->removeItem($id)
->save();
$this->_getSession()->setCartWasUpdated(true);
if (!$this->_getCart()->getQuote()->getHasError()){

$response[’status’] = ‘SUCCESS’;
$response[’message’] = $message;

$this->loadLayout();
$sidebar_block = $this->getLayout()->getBlock(’cart_sidebar’);
Mage::register(’referrer_url’, $this->_getRefererUrl());
$sidebar = $sidebar_block->toHtml();
$total = Mage::helper(’checkout/cart’)->getCart()->getItemsCount();
$response[’total’] = $total;
$response[’checkout’] = $sidebar;


}

$this->getResponse()->setBody(Mage::helper(’core’)->jsonEncode($response));
return;
} catch (Exception $e) {
$this->_getSession()->addError($this->__(’Cannot remove the item.’));
Mage::logException($e);
}
}

}

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top