1

I'm working on a site where we have two types of customers, agents and sites. A customer logs on as an agent then chooses a site that they can order for (these are in a custom customer attribute). The agent customer adds items to the site customer basket and can check out (the site user selection actually changes the customer session from being the agent customer to the site customer).

Now, if the agent customer has items in their basket and then changes the site customer we want to keep the basket contents intact but because they are in reality changing the logged on customer from one site customer to another the basket contents are lost. Anyone have any ideas on how I can keep the basket contents intact between site customers?

1
  • Why not change the customer_id of the quote, when the customer changes? Mar 6 '13 at 10:51
4

Changing the customer_id of the quote:

Mage::getModel('checkout/quote')->load($quoteId)->setCustomerId($customerId)->save();
4
  • The problem I can see with that is that the quote also stores things like the customer email etc in it so there is a strong possibility of data integrity issues. I'll certainly give it a go though and see what happens. Mar 6 '13 at 11:21
  • 1
    Actually, that does seem to work thanks Fabian. I just grab the quoteID and then use this after the new customer has been logged in and assign the cart to them, The basket is then intact and ready for checkout for the new site customer. One small change was that it shoud have been Mage::getModel('sales/quote') not checkout/quote but apart from that it seems to work perfectly. Mar 6 '13 at 13:00
  • Hi Fabian How to achieve same on Magento 2?? can you please help? Dec 9 '16 at 12:18
  • I think this has some race conditions, when both customers are accessing it at the same time. @aton1004 no clue, ask another question for M2 Dec 11 '16 at 16:53
3

No experience with this what so ever and this code is far from tested OR complete but you can probably use an event observer (not checkout_cart_product_add_after or you'll have yourself an infinite add loop) that is triggered when adding the product and then execute something like this

$customer_session = Mage::getSingleton('customer/session')->setCustomer([customerid]);
$cart_session = Mage::getSingleton('checkout/session')->loadCustomerQuote();

$cart = Mage::getSingleton('checkout/cart’'); 
$cart->init(); 

$product = Mage::getSingleton('catalog/product')->load($productId); 

$cart->addProduct($product, array('qty' => $qty)); 
$cart->save(); 

$cart_session->setCartWasUpdated(true);
3
  • If you manage to get it working please share it, it's actually quite an interesting case.
    – Sander Mangel
    Mar 6 '13 at 9:52
  • Thanks Sander, I'll have a play around with it and see how that works and I'll share the finished solution (if it all works). Mar 6 '13 at 10:12
  • Great, let me know if you need any help if you run into an issue
    – Sander Mangel
    Mar 6 '13 at 10:18
2

@'Fabian Blechschmidt' for the answer for Sharing the shopping cart. I have tried your code, but need some modifications in it, it should use 'sales/quote' instead of 'checkout/quote', after that it is working fine for me. I have used below code:

Mage::getModel('sales/quote')->load($quoteId)->setCustomerId($customerId)->save();

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.