本文介绍了Magento-Sku从购物车中删除物品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从Sku的购物车中移除产品-可以吗?

i try to remove a product from Cart by Sku - is it possible?

我在cartcontroller.php中尝试了以下代码,但没有成功....

I tried the following Code in cartcontroller.php but without success ....

我知道它应该按ID运行,但按Sku对我来说会更容易.

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

推荐答案

尝试

$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;
    }
}

请参阅如何从Magento的报价中删除项目?

这篇关于Magento-Sku从购物车中删除物品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:11