/app/code/local/Namespace/Cart/etc/config.xml
<events>
<load_customer_quote_before>
<observers>
<module_load_customer_quote_before>
<class>Namespce_Cart_Model_Observer</class>
<method>clearCarts</method>
</module_load_customer_quote_before>
</observers>
</load_customer_quote_before>
</events>
</events>
/app/code/local/Namespace/Cart/Model/Observer.php
<?php
class Namespace_Cart_Model_Observer extends Varien_Object
{
public function clearCarts(Varien_Event_Observer $observer)
{
$lastQuoteId = Mage::getSingleton('checkout/session')->getQuoteId();
if ($lastQuoteId) {
$customerQuote = Mage::getModel('sales/quote')
->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
$customerQuote->setQuoteId($lastQuoteId);
$this->_removeAllItems($customerQuote);
} else {
$quote = Mage::getModel('checkout/session')->getQuote();
$this->_removeAllItems($quote);
}
}
protected function _removeAllItems($quote){
foreach ($quote->getAllItems() as $item) {
$item->isDeleted(true);
if ($item->getHasChildren()) {
foreach ($item->getChildren() as $child) {
$child->isDeleted(true);
}
}
}
$quote->collectTotals()->save();
}
}
<events>
<load_customer_quote_before>
<observers>
<module_load_customer_quote_before>
<class>Namespce_Cart_Model_Observer</class>
<method>clearCarts</method>
</module_load_customer_quote_before>
</observers>
</load_customer_quote_before>
</events>
</events>
/app/code/local/Namespace/Cart/Model/Observer.php
<?php
class Namespace_Cart_Model_Observer extends Varien_Object
{
public function clearCarts(Varien_Event_Observer $observer)
{
$lastQuoteId = Mage::getSingleton('checkout/session')->getQuoteId();
if ($lastQuoteId) {
$customerQuote = Mage::getModel('sales/quote')
->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
$customerQuote->setQuoteId($lastQuoteId);
$this->_removeAllItems($customerQuote);
} else {
$quote = Mage::getModel('checkout/session')->getQuote();
$this->_removeAllItems($quote);
}
}
protected function _removeAllItems($quote){
foreach ($quote->getAllItems() as $item) {
$item->isDeleted(true);
if ($item->getHasChildren()) {
foreach ($item->getChildren() as $child) {
$child->isDeleted(true);
}
}
}
$quote->collectTotals()->save();
}
}
No comments:
Post a Comment