0

Here is how the cart is cleared for the current session user:

$cart = Mage::getSingleton('checkout/cart'); 
$quoteItems = Mage::getSingleton('checkout/session')
                  ->getQuote()
                  ->getItemsCollection();

foreach( $quoteItems as $item ){
    $cart->removeItem( $item->getId() );    
}
$cart->save();

My question is how can i clear cart by user id, not by session?

2 Answers 2

1

Take a look in app/code/core/Mage/Sales/Model/Quote.php. On line :354 you find the method loadByCustomer(). You can call it like:

$quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
$quoteItems = $quote->getAllItems();

$customer could be the customer Model or the customer Id

0

You can remove specific customer cart by customer id or email address

$customer = Mage::getModel('customer/customer')->setWebsiteId(Mage::app()->getWebsite()->getId());

$customerId = $customerEmail = '';
       if ( isset($_REQUEST['customerId']) ) {
        $customerId = $_REQUEST['customerId'];
       }

       if ( isset($_REQUEST['customerEmail']) ) {
        $customerEmail = $_REQUEST['customerEmail'];
       }

if (isset($customerId) and $customerId != '') {  
    $customer = $customer->load($customerId);
} elseif (isset($customerEmail) and $customerEmail != '' ) {
    $customer = $customer->loadByEmail($customerEmail);
} else {
               echo "customerId or customerEmail parameter required";
               exit;
       }

if ($customer->getId()) {

       $storeIds = Mage::app()->getWebsite(Mage::app()->getWebsite()->getId())->getStoreIds();

      $quote = Mage::getModel('sales/quote')->setSharedStoreIds($storeIds)->loadByCustomer($customer);

    if ($quote) {
        $collection = $quote->getItemsCollection(false);

        if ($collection->count() > 0) {
            foreach( $collection as $item ) {
                if ($item && $item->getId()) {
                    $quote->removeItem($item->getId());
                    $quote->collectTotals()->save();
                }
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.