Magento – Detach quote from customer session

catalogcustomersession

I need to somehow detach the quote from the customer session so it is still sitting in the DB marked as active but no longer part of the customer session (still with the customer_id in the DB record though). I've been scouring all the customer session and quote code but can't see anything obvious that helps me implement this.

Anyone got any ideas?

Best Answer

If you're looking to reset the current customer's quote to a blank quote (e.g. to 'save' their cart for later) the following should suffice (untested code):

To change the current quote:

$quote_id = /*your quote id here*/ ;
Mage::getSingleton('checkout/session')->setQuoteId($quote_id);

To get rid of the current quote altogether:

Mage::getSingleton('checkout/session')->setQuoteId(null);

Edit

If you want to forcefully reattach a customer's quote to another customer, you can change the customer_id of the sales_flat_quote field:

For registered customers:

$quote->setCustomerId($customer_id);

For guests:

$quote->setCustomerId(null)
    ->setCustomerEmail($email)
    ->setCustomerFirstname($firstname)
    ->setCustomerLastname($lastname)
    ->save();
$quote_id = $quote->getId();
Mage::getSingleton('checkout/session')->setQuoteId($quote_id);