简体   繁体   English

在 Magento 中添加新项目之前清除购物车中的现有项目

[英]Clear existing items in cart before adding a new Item in Magento

I am creating a new cart addAction() in magento's checkout/controllers/cartController.php.我正在 magento 的 checkout/controllers/cartController.php 中创建一个新的购物车 addAction()。 My need is, Whenever a product is added to cart, all existing products in cart should be cleared except currently added one.我的需要是,每当将产品添加到购物车时,应清除购物车中的所有现有产品,但当前添加的产品除外。 That is,那是,

If Nokia 3220 is added to an empty cart, It should be placed as an item.如果将诺基亚 3220 添加到空购物车中,则应将其作为项目放置。

And Later if Nokia N72 is added, Nokia 3220(ie the previous item) should be cleared from cart and Nokia N72 should be placed and so on.之后如果添加诺基亚 N72,则应将诺基亚 3220(即上一项)从购物车中清除并放置诺基亚 N72,依此类推。

I override the CartController.php and added the code to begining of addAction() in CartController.php,我覆盖了 CartController.php 并将代码添加到 CartController.php 中 addAction() 的开头,

$checkout_my_cart = Mage::getSingleton('checkout/cart'); $checkout_my_cart = Mage::getSingleton('checkout/cart'); $current_items = $checkout_my_cart->getItems(); $current_items = $checkout_my_cart->getItems(); foreach ($current_items as $item) { $itemId = $item->getItemId(); foreach ($current_items as $item) { $itemId = $item->getItemId(); $checkout_my_cart->removeItem($itemId)->save(); $checkout_my_cart->removeItem($itemId)->save(); } }

But It clears the entire cart when I add a new item to replace existing one!但是当我添加一个新项目来替换现有项目时,它会清除整个购物车! I think It should not do it since I added the code in the begining of Addaction().我认为它不应该这样做,因为我在 Addaction() 的开头添加了代码。 I tried by defining the above code as a function and calling it with addAction().我尝试将上述代码定义为函数并使用 addAction() 调用它。 But story seems to be the same.但故事似乎是一样的。

Any help will be appreciated.任何帮助将不胜感激。

Please Help.请帮忙。

You should consider using observer rather then overriding controller.您应该考虑使用观察者而不是覆盖控制器。 If you look at the addItem method within Mage_Sales_Model_Quote you will find that event sales_quote_add_item .如果您查看Mage_Sales_Model_Quote中的addItem方法,您会发现该事件sales_quote_add_item

You can observe it, fetch the quote_item (argument to event) and delete items other then this one (was typing by hand so please double check for errors):您可以观察它,获取quote_item (事件的参数)并删除quote_item项目(手动输入,因此请仔细检查错误):

public function observeAddItem($observer) 
{
  $item = $observer->getEvent()->getQuoteItem();
  $cart = Mage::getSingleton('checkout/cart');
  foreach ($cart->getQuote()->getItemsCollection() as $_item) {
    if($_item->getId() != $item->getId()) {
      $_item->isDeleted(true);
    }
  }
}

Overriding classes (especially controllers) should be your last resort.覆盖类(尤其是控制器)应该是你最后的手段。

More info on how to observe event can be found on magento wiki:更多关于如何观察事件的信息可以在 magento wiki 上找到:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/customizing_magento_using_event-observer_method

Hi please check below code:您好,请检查以下代码:

$session= Mage::getSingleton('checkout/session');
$quote = $session->getQuote();
$cart = Mage::getModel('checkout/cart');
$cartItems = $cart->getItems(); 
foreach ($cartItems as $item){
    $quote->removeItem($item->getId())->save();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM