Magento – How to clear a specific user cart items without session

ce-1.9.2.0magento-1.9session

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?

Best Answer

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

Related Topic