Magento - Remove Item from Cart by Sku

2019-08-27 21:38发布

问题:

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();

回答1:

Super close... get the product

if ($item->getProduct()->getSku() == promo){


回答2:

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?



标签: magento cart