In Magento 2.4.4, I saw entries in catalog_compare_item as visitor_id = 0 and customer_id = NULL, How visitor id is updating in the table?
In Magento 2.4.4, the visitor_id in the catalog_compare_item table is set based on the value stored in the visitor cookie.
Whenever a visitor comes to the website, Magento sets a visitor cookie with a unique ID. This visitor ID is used to associate all of the visitor's activity on the website, such as product views, add to cart, etc. If the visitor adds a product to their compare list, Magento uses the same visitor ID to store the comparison data in the catalog_compare_item table.
If the visitor_id in the catalog_compare_item table is 0, it means that Magento was not able to retrieve the visitor ID from the visitor cookie. This can happen if the visitor has disabled cookies or if there was some issue in setting or reading the cookie. Similarly, if the customer_id is NULL, it means that the visitor is not logged in as a customer.
When a visitor adds a product to the compare list, the system generates a unique visitor_id for that visitor if one does not already exist. This visitor_id is stored in the catalog_compare_item table along with the product information.
The visitor_id is generated by the Magento\Framework\Stdlib\Cookie\PhpCookieManager class, which sets a cookie named mage-cache-sessid to the visitor's browser. This cookie is used to store the visitor_id and is set to expire when the visitor closes their browser.
When a visitor adds a product to the compare list, the system checks if the mage-cache-sessid cookie is already set. If it is, the system retrieves the visitor_id from the cookie and associates the compare list item with that visitor_id. If the cookie is not set, the system generates a new visitor_id, sets the mage-cache-sessid cookie, and associates the compare list item with the new visitor_id.
So, if you see entries in the catalog_compare_item table with visitor_id = 0 and customer_id = NULL, it means that the compare list item was added by a visitor who did not have a mage-cache-sessid cookie set. Hence, a new visitor_id was generated for them. Additionally, the visitor was not logged in as a customer at the time of adding the product, so the customer_id field is empty.