3

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));
  }
Reid Blomquist
  • 1,413
  • 10
  • 19
HungDQ
  • 802
  • 10
  • 31
  • Please clarify your question. To what does "one item in cart" refer to? This assumes they've already added an item. Why isn't the default cart adjustment behavior sufficient? – Reid Blomquist Nov 10 '14 at 02:55
  • 1
    It like this website: https://www.goodeggs.com/sfbay/produce . When you click on plus or minus, the number of this product in cart will be changed. – HungDQ Nov 10 '14 at 03:02

2 Answers2

2

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;

    }
Amit Bera
  • 77,456
  • 20
  • 123
  • 237
1

The example posted is using ajax/JSON - you'll have to first change Magento cart behavior to ajax add to cart (if you haven't already) and then simply create a remove from cart ajax action as well. This may help you get started: http://excellencemagentoblog.com/magento-add-product-to-cart-ajax

Thanks for the edit! It looks to me as though you're never associating the product id to the cart item to update - but are instead simply loading the product model for that ID. What about trying something like:

public function removeAction() {
     $this->getResponse()->setHeader('Content-type', 'application/json');
     $_params = $this->getRequest()->getParams();
     $_cart = Mage::getSingleton('checkout/cart');
     $_items = $_cart->getItems();
     foreach ($_items as $item) {
          if($item->getId() == $_params['id]) {
               $a = $item->getItemsQty();
               $item->setQty($a-1);
               $cart->save();
         }
    }
    return $this->getResponse()->setBody(json_encode($_response));
}
ND17
  • 5,181
  • 9
  • 50
  • 78
Reid Blomquist
  • 1,413
  • 10
  • 19
  • I already using ajax cart but I can't find anyway to create a function that can remove per item from cart. I try to get item qty and - 1 then setQty = Qty - 1 but my cart is doesn't change. – HungDQ Nov 10 '14 at 03:15
  • There's no really easy way for me to help you with this unless you provide code/code snippets to make your issue re-creatable. – Reid Blomquist Nov 10 '14 at 03:23
  • I just update my function in my post, can you see that ?. Thank you very much !! – HungDQ Nov 10 '14 at 03:31
  • just edited - see if this helps at all – Reid Blomquist Nov 10 '14 at 03:49
  • It's very useful but doesn't work. I try to dump $item->getId() and get value is "13" but my product id is "12". I don't know why ? – HungDQ Nov 10 '14 at 04:02
  • Is it an increment of 1 off for all items you try? Maybe check that and debug from there, could be helpful to see what a dump of $_params looks like – Reid Blomquist Nov 10 '14 at 04:09