Have you stuck with count items cart in top cart of magento site ? In my case, I need to remove all items cart when customer place order via frontend magento.
After trying remove all items in cart but the number count items in cart is not updated. It remains the same count items as before removing items cart.
So here is the solution for it.
Overall, I use an observer “checkout_onepage_controller_success_action” to write down my function which removes items carts and update count items cart correctly.
In this case, I create new module : Inbusiness_Checkout.
1/ Declare new module : app/etc/modules/Inbusiness_Checkout.xml
<?xml version="1.0"?> <config> <modules> <Inbusiness_Checkout> <active>true</active> <codePool>local</codePool> </Inbusiness_Checkout> </modules> </config>
2/In app/code/local/Inbusiness/Checkout/config.xml
<?xml version="1.0"?> <config> <modules> <Inbusiness_Checkout> <version>1.1.1</version> </Inbusiness_Checkout> </modules> <global> <events> <!-- here the event to hook: --> <checkout_onepage_controller_success_action> <observers> <Inbusiness_Checkout_Clear> <type>model</type> <class>Inbusiness_Checkout_Model_Observer</class> <method>clearCart</method> </Inbusiness_Checkout_Clear> </observers> </checkout_onepage_controller_success_action> </events> </global> </config>
3/Create an Observer.php at : app/code/local/Inbusiness/Checkout/Model/Observer.php
<?php class Inbusiness_Checkout_Model_Observer{ public function clearCart($observer){ $checkout_cart = Mage::getSingleton('checkout/cart'); $items = $checkout_cart->getItems(); foreach ($items as $item) { $itemId = $item->getItemId(); try { $checkout_cart->removeItem($itemId); }catch (Exception $e) { echo $this->__('Cannot remove the item.'); } } $checkout_cart->save(); Mage::getSingleton('checkout/cart')->truncate()->save(); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); Mage::getSingleton('core/session', array('name'=>'frontend')); $quoteID = Mage::getSingleton("checkout/session")->getQuote()->getId(); if($quoteID) { try { $quote = Mage::getModel("sales/quote")->load($quoteID); $quote->setIsActive(false); $quote->delete(); } catch(Exception $e){throw $e;} } Mage::getSingleton('checkout/session')->clear(); } } ?>
Hope it helps.
COMMENTS