Magento – Removing Items from the cart doesn’t update the ajax cart

ajaxcartmagento-1.9

I've got a problem while deleting an item from the cart.

When I try to delete any item from the cart it is updating the normal cart page.

But the item is there in the Ajax mini-cart though the quantity of the Ajax mini-cart is decreased.

But the problem doesn't persist while I change the quantity in both the carts, and they are synced pretty well except removing item.

Please help me out. I'm using Magento version 1.9, Any help would be appreciated.

Found out : When I use location.reload(true); in my onsuccess call back the page is getting reloaded and I got the desired output. But I don't wanna reload the page (That's why we use AJAX!!!) Please help me…

Best Answer

Use this code

Create a delete action in your ajax add to cart controller.

Your Controller Action

    public function deleteAction()
    {
        if((int) $this->getRequest()->getParam('isAjax') == 1){
            $id = (int) $this->getRequest()->getParam('id');
            if ($id) {
                try {
                    $this->_getCart()->removeItem($id)->save(); 
                    $message = $this->__('Item was removed from your shopping cart.');
                    $response['status'] = 'SUCCESS';
                    $response['message'] = $message;
                    $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
                    return;

                } catch (Exception $e) {
            $response['status'] = 'ERROR';
            $response['message'] = $this->__('Cannot remove the item from shopping cart.');
            Mage::logException($e);
                }
            }
        } else {
            return parent::deleteAction();
        }
    }

Add the JS and button code in the app/design/frontend/[YOUR THEME]/[YOUR THEME]/checkout/cart/item/default.phtml

Js Function

    <script type="text/javascript">
        var deleting = 0;
        productRemoveFromCart = function(url, elem){
            deleting = deleting + 1;
            jQuery(elem).parent().parent().animate({ opacity: 0.5 }, 200, function() { });
            if (!url) {
                    //alert('Problem');
            }
            url = url.replace("checkout/cart","ajax/index");
            if(window.location.protocol == "https:") {
                url = url.replace("http:","https:");
            }
            url += '&isAjax=1';
            var data = 'isAjax=1';
            try {
                    jQuery.ajax( {
                            url : url,
                            dataType : 'json',
                            type : 'post',
                            data : data,
                            success : function(data) {
                                    deleting = deleting - 1;
                                    if(deleting<1){
                                        var pagepath = window.location.pathname;
                                        if((pagepath.indexOf("onestepcheckout")>0)||(pagepath.indexOf("checkout/cart")>0)){
                                            location.reload();
                                        }
                                    }
                            },
                            error: function (data) {
                                deleting = deleting - 1;
                            }
                            }); 
            } catch (e) {
            }
        }     
</script>

Button code

     <td class="a-center"><a href="javascript:void(0)" onclick="productRemoveFromCart('<?php echo $this->getDeleteUrl(); ?>',this);" title="<?php echo $this->__('Remove item')?>" class="btn-remove btn-remove2"><?php echo $this->__('Remove item')?></a></td>