Reputation: 101
i try to remove a product from Cart by Sku - is it possible?
I tried the following Code in cartcontroller.php but without success ....
I know it should work by ID but by Sku would be easier for me.
$session = Mage::getSingleton('checkout/session');
$quote = $session->getQuote();
$cart = Mage::getModel('checkout/cart');
$cartItems = $cart->getItems();
foreach($cartItems as $item) {
if ($item->getSku() == promo){
$quote->removeItem($item->getId())->save();
}
}
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$cart->init();
Upvotes: 0
Views: 6764
Reputation: 17656
Try
$cartHelper = Mage::helper('checkout/cart');
$items = $cartHelper->getCart()->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getSku() == 'promo') {
$cartHelper->getCart()->removeItem($item->getItemId())->save();
break;
}
}
See How to remove item from quote in Magento?
Upvotes: 1
Reputation: 6275
Super close... get the product
if ($item->getProduct()->getSku() == promo){
Upvotes: 1