I’m trying to create different checkout sessions for the logged in customer at a store level, rather than at the site level.
We have a site with 2 stores. When a user adds a product to the cart, we want to show that product in the cart only for that store. But currently switching between stores will show up products of other stores previously added to the cart.
After some research I found that whenever a user logs in to a store, a record is created in the sales_flat_quote table for the logged in customer, which is common for all the stores in that site. So I need to create a new record in the sales_flat_quote table when the user switches stores.
I found that the loadByCustomerId() function in class Mage_Sales_Model_Mysql4_Quote is what creates the new record in the sales_flat_quote table. But all I see is a select query:
$read = $this->_getReadAdapter();
$select = $this->_getLoadSelect('customer_id', $customerId, $quote)
->where('is_active=1')
->order('updated_at desc')
->limit(1);
$data = $read->fetchRow($select);
I don’t understand how the select query is creating the record in the sales_flat_quote table.
Could someone guide as to how to create a new record in this table for the logged in customer when he/she switches to an other store under the same site?
Which is correct.
It looks like you want to separate carts for
storedefinitions, which both are connected to the very samewebsitedefinition in Magento.In Magento, all
stores defined within the samewebsitedo always share the very same cart.That’s by-design.
To achieve cart separation you would need to change your Magento setup to have two
websites and onestoreper website. Otherwise you’ll run into endless issues separating the two stores (like the issue you’re having now), I guess.That said, I’ll nevertheless try to answer your concrete question:
A
SELECTquery usually never ever will create a record; it’s thought to read data.Have a look at
Mage_Checkout_Model_Session::loadCustomerQuote():This is what leads to the call of
Mage_Sales_Model_Mysql4_Quote::loadByCustomerId()you’re talking about. It just loads the customers quote (if any), nothing else.Creation/update of the record happens by calling the quotes
save()method.Magento EE 1.11.0.0 for example saves the record this way:
You didn’t mention the Magento version you use, so your mileage may vary, but the principle will still remain the same.