3

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?

  • Can you be more specific in what are you trying to do? Quotes are detached from the customer sessions: they outlive sessions and can also be created via api when there is no customer session present. Let us know what are you trying to achieve so we can point you to the right way to do it. – Paul Grigoruta Mar 20 '13 at 16:57
  • Basically, I have a system where logged in customers can change which customer they are logged in as by selecting a new customer from a dropdown. This does not destroy the customer session as they have to be logged in to be able to switch the customer they are logged in as. So, if I switch customer in the session I need to be able to lay aside the cart items so if they switch back to the previous customer record their cart will be regenerated. – Richard Cleverley Mar 20 '13 at 17:08
  • If I understand this correctly, they have different identities with the same customer_id? Having different IDs would solve your problem perfectly, then you just change the customer in the session: magento.stackexchange.com/questions/1161/… – Fabian Blechschmidt Mar 24 '13 at 16:23
4

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);
| improve this answer | |
  • Hmm possibly that may work, was thinking along the same lines just now. I'll give that a go and see if it works. – Richard Cleverley Mar 20 '13 at 17:17
  • Let me know. I'll keep digging as well. – philwinkle Mar 20 '13 at 17:40
  • It certainly works up to a point and I can retrieve the quote_id using Mage::getSingleton('checkout/session')->loadCustomerQuote()->getQuoteId() but it doesn't seem to reattach when I set the quoteId in the checkout session. Must be nearly there though. – Richard Cleverley Mar 20 '13 at 18:10
  • Mage::getSingleton('checkout/session')->setQuoteId(null); is not working any idea? – Keyur Shah Oct 1 '13 at 5:59
  • When you say it's not working, what exactly is not working? – philwinkle Oct 3 '13 at 5:58
1

I've just had to quickly do this for a customer - and with a non-programatic solution, you can just replace the quote_id for the items in the old quote.

Eg.

UPDATE sales_flat_quote_item SET quote_id = 'NEW_QUOTE_ID' WHERE quote_id = 'OLD/EXISTING_QUOTE_ID'

Replace __NEW_QUOTE_ID__ and __OLD/EXISTING_QUOTE_ID__ as necessary

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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