Magento – How to remove or reduce cart item qty via AJAX

cart

I have to add two button minus and plus in list product page like this site: https://www.goodeggs.com/sfbay/produce .

Each time an user click on minus button, the number of this product in cart will be change.
I can't find any solution to do this. Who can help me ?.

Here is my remove function :

public function removeAction() {
    $this->getResponse()->setHeader('Content-type', 'application/json');
    $_params = $this->getRequest()->getParams();
    $_product = Mage::getModel('catalog/product')->load($_params['id']);
    $_cart = Mage::getSingleton('checkout/cart');
    $a = $_cart->getItemsQty();
    $a = $a - 1;
    $_cart->setQty($a);
    $_cart->save();
    return $this->getResponse()->setBody(json_encode($_response));
  }

Best Answer

you need to check this product id is exiting on your cart current cart

$this->getResponse()->setHeader('Content-type', 'application/json');
    $_params = $this->getRequest()->getParams();
    $_product = Mage::getModel('catalog/product')->load($_params['id']);
    $cart =  Mage::getModel('checkout/cart')->getQuote();
$_response=array()
    foreach ($cart->getAllItems() as $item) {
        if($item->getSku()==$_product->getSku()):

                $quoteItem = $cart->getQuote()->getItemById($item->getId());
                if (!$quoteItem) {
                   $_response['message']=$this->__('Quote item is not found.');
                }

                if($quoteItem){
        $a=$quoteItem->getQty()
                 $quoteItem->setQty($a-1)->save();
                }

    endif;

    }