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?
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.
Which is correct.
It looks like you want to separate carts for store definitions, which both are connected to the very same website definition in Magento.
In Magento, all stores defined within the same website do 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 one store per 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:
I don't understand how the select query is creating the record in the
sales_flat_quote table.
A SELECT query usually never ever will create a record; it's thought to read data.
Have a look at Mage_Checkout_Model_Session::loadCustomerQuote():
$customerQuote = Mage::getModel('sales/quote')
->setStoreId(Mage::app()->getStore()->getId())
->loadByCustomer(Mage::getSingleton('customer/session')->getCustomerId());
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:
if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
if ($this->getQuoteId()) {
$customerQuote->merge($this->getQuote())
->collectTotals()
->save(); // <- either this
}
$this->setQuoteId($customerQuote->getId());
if ($this->_quote) {
$this->_quote->delete();
}
$this->_quote = $customerQuote;
} else {
$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();
$this->getQuote()->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
->setTotalsCollectedFlag(false)
->collectTotals()
->save(); // <- or that does the save
}
You didn't mention the Magento version you use, so your mileage may vary, but the principle will still remain the same.
Related
We are facing a issue in Magento 2.4. We are using MSI and we have 3 warehouses. All products have assigned 3 warehouse and we manage stock using MSI. A product can be available on one warehouse and can't be available on another, The issue is when we get product out of stock one day it get automatically in stock after 1 or 2 days and its random behavior not with specific products or warehouse.
Initially we thought may be its done by someone in team if they worked on same product so we have setup an alert when ever product get changed by admin we get notification on Email. But the strange thing is without any alert still status get changed.
I have used this event controller_action_catalog_product_save_entity_after to trigger an alert when ever product get changed and its working as well we have tested it.
Although we have restrict import functionality for other users, but we think product may be changed through csv or api although we have restricted but may be it can be done from any user.
Can any one please help if someone faced similar issue or any model function which always trigger when ever product stock status get changed from any action like API or from CSV or from Admin Edit or even frontend ??
I believe you can use Magento event catalog_product_save_after. Create an observer method that does the following on event catalog_product_save_after.
public function catalog_product_save_after($observer) {
$product = $observer->getProduct();
$stockData = $product->getStockData();
if ( $product && $stockData['qty'] ) {
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getEntityId()); // Load the stock for this product
$stock->setData('is_in_stock', 1); // Set the Product to InStock
$stock->save(); // Save
}
}
I am trying to build an ecommerce site.for the cart functionality I want to alive my cart for multiple devices.For example if I create an account and add some product to my cart using my mobile browser,after that if I use the same account to my pc browser I should see the cart with those product I added using my mobile browser.I donno how to do that or what's the method,can you please help me??
My Preferred Approach
In order to do this, you'll want to create or leverage some sort of profile data source. For example, if you're using a database, you likely have a Users table with a record for each customer. I'd suggest adding a new related table that might look something like this to begin with...
Potential Table Name: UserData
UserID (FK)
DataName (string)
DataValue (string)
So, after logging in, the visitor might add something to their cart. When this happens, you'd have a CartID to work with and then add a record to that table with their UserID and it might look like this in T-SQL.
INSERT INTO [dbo].[UserData] ([UserID],[DataName],[DataValue])
VALUES (12345, 'CartID', 'd501e3de-350c-4c20-92c8-8e71445e1774');
Now, when they log in again anywhere, you'd just query for that UserID to find out if they have a cart pending. If they do, load it up. Just remember to clear that value on checkout.
The beauty of this solution is that you can repurpose it for all kinds of other things.
An Alternative Approach
Just query your Cart table, assuming in your solution you're storing carts before checkout is completed. Find the most recent instance of a cart matching their UserID that hasn't been completed.
I have a client who is experiencing issues with their Wordpress/WooCommerce website.
Issue: The website is automatically adding products to the cart (Roughly 40-60). The issue occurs for both logged-in & logged-out users. The issue can occur when trying to login to the My Account section, when adding items to the cart or sometimes after adding items to the cart and then visiting the cart it will override cart items with new random items.
I have noticed that in WC_Session_handler the value for _customer_id is often not unique nor is the other session data.. I have removed all server & front-end caching, searched for any other sessions initialized.
Any help would be appreciated as they are losing business due to customers not being able to remove the items from their cart (As the removed items re-appear quickly)..
Domain Name: thecoffeehopper.com :)
The cart is constructed from data in the wp_woocommerce_sessions and wp_usermeta MySQL tables.
I would run the following sql queries to try and find the source of your problem.
select * from wp_woocommerce_sessions;
select * from wp_usermeta where meta_key like '_woocommerce_persistent_cart_%';
The data from these rows are used to construct the cart. Are the random items found in any of these rows? The rows have a user id or customer id to tell which customer the data belongs to.
The data in these rows are serialized strings and is difficult to read directly so I would use the WordPress CLI tool and apply the function maybe_unserialize() to the SQL results.
I have a lot of archived products that I would like to delete.
But I am using a function in the frontend where users can see their old orders and what products were bought:
$orders = Mage::getResourceModel('sales/order_grid_collection')
->addFieldToFilter('store_id', $storeId)
->load();
Some of the products in the orders are already archived.
When I delete all archived products, can the products inside the orders still be accessed? Or will I lose those products?
As answered by Marius:
"Normally, the orders don't have only references to the order products, but they also keep product values (that might seam redundant) because you want to see a snapshot of the product you ordered at the time you ordered it.
This way you avoid seeing new prices or descriptions.
The order history section that magento offers by default works even if you delete ordered products.
but if you have a custom code that loads a product collection or a product to get additional info, it will be affected if you delete the products.
If the only code you use is the one you shown in the question you should be save.
I suggest trying to delete the products first on a staging server then do it on live.
and backup before doing anything."
(If you want to upvote, please consider upvoting his answer too)
Through which file are new customers created and saved in Magento databases?
Which function is used?
Please provide me with full path locations and function names.
Also tell me about customer groups. From where and through which files/functions do customer groups get saved in Magento?
How are customers and customer groups related?
How can I "catch" customer IDs and customer group IDs from those files?
I find the file through which Magento saves New Customer-:
/magento/app/code/core/Mage/Adminhtml/controllers/CustomerController.php
In the file CustomerController.php, there is a function saveAction() which saves a customer in Magento db.
I wanted to know how to catch Newly created customer's id(I guesss i.e. Entity Id in Customer_entity table for Magento db) from that CustomerController.php file.
I tried this but it won't work for me-"
$customer_data = Mage::getModel('customer/customer')->load($customer_id);
Anybody knows how to catch customer id while creation of new customer???
Try to search here:
Mage_Customer_AccountController->createPostAction
Well class name means
(root)/app/code/core/Mage/Customer/controllers/AccountControlller
Mage_Adminhtml_Customer_GroupController
Take a look at the AccountController.php at app/code/core/Mage/Customer/controllers, it will give you an idea how customer registration is processed.
Basically an instance of Mage_Customer_Model_Customer is created, proper data is assigned to it (collected from a post form on the registraton form or received via a webservice call) and the model is saved. The internal Magento mechanics work to put this data in proper db tables.
See also create function of the Mage_Customer_Model_Customer_Api class.
customer groups are created in Mage_Adminhtml_Customer_GroupController->saveAction(), the model being used is Mage_Customer_Model_Group
actually customer group is assigned from backend,by default general group is selected for customer group, if you want to get customer groups then you can use this code Mage::helper('customer')->getGroups()->toOptionArray();
Search for this file
/YourProject/app/code/core/Mage/Customer/controllers/AccountController.php
Search for this function name
public function createPostAction() //Magento saves New Customer
This above mentioned function is responsible for new customer registration.
print_r($this->getRequest()->getPost()); //get all the post data
print_r($session); //get all the session data