Magento - Remove Item from Cart by Sku

好久不见. 提交于 2019-12-13 04:30:55

问题


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?



来源:https://stackoverflow.com/questions/18191549/magento-remove-item-from-cart-by-sku

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!