I am trying to remove just one qty from my cart rather than all, but to no avail.
Can anybody help?
Here is the code I have got so far...
require_once 'app/Mage.php';
Mage::app("default");
Mage::getSingleton("core/session", array("name" => "frontend"));
$session = Mage::getSingleton("customer/session");
$yourProId = $_POST['prodID'];
$qty = 1;
foreach (Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item) {
if ($yourProId == $item->getProductId()) {
Mage::getSingleton('checkout/cart')->removeItem($item->getId())->save();
}
}
UPDATE: Here is the code that works, thanks to R.S. for this!
$yourProId = $_POST['prodID'];
$qty=1;
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $yourProId) {
$qty = $item->getQty() - 1; // check if greater then 0 or set it to what you want
if($qty == 0) {
Mage::getSingleton('checkout/cart')->removeItem($item->getId());
} else {
$item->setQty($qty);
}
$cartHelper->getCart()->save();
break;
}
}
Try
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $yourProId) {
if( $item->getQty() == 1 ){
$cartHelper->getCart()->removeItem($item->getItemId())->save();
}
else if($item->getQty() > 1){
$item->setQty($item->getQty() - 1)
$cartHelper->getCart()->save();
}
break;
}
}
Take a look @ /app/code/core/Mage/Checkout/controllers/CartController.php