{"id":1434,"date":"2016-12-22T21:26:50","date_gmt":"2016-12-22T15:26:50","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=1434"},"modified":"2021-10-23T13:47:07","modified_gmt":"2021-10-23T18:47:07","slug":"how-a-quote-is-written-to-sales_flat_quote-in-magento-1-x","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/how-a-quote-is-written-to-sales_flat_quote-in-magento-1-x\/","title":{"rendered":"How a Quote is Written to sales_flat_quote in Magento 1.x"},"content":{"rendered":"<p>I was trying to troubleshoot an issue with quotes in Magento 1.x the other day and did some digging through the core code. &nbsp;I now have a much better understanding of what the process looks like when a quote is written to the&nbsp; <code>sales_flat_quote<\/code>&nbsp;table. &nbsp;The process is a bit complex and has some unique items to it that I didn&#8217;t expect to see. &nbsp;For that reason I thought I&#8217;d document the process in <em>clear English<\/em>&nbsp;in the hopes that it helps someone else better understand the process as well.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/12\/Magento-1x-Writing-Customer-To-Quote-In-Database.jpg\" rel=\"attachment wp-att-440\" data-lasso-id=\"625\" data-rel=\"lightbox-gallery-WX0mDGuR\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/12\/Magento-1x-Writing-Customer-To-Quote-In-Database-500x500.jpg\" alt=\"\" class=\"wp-image-1440\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/12\/Magento-1x-Writing-Customer-To-Quote-In-Database-500x500.jpg 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/12\/Magento-1x-Writing-Customer-To-Quote-In-Database-150x150.jpg 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/12\/Magento-1x-Writing-Customer-To-Quote-In-Database-768x768.jpg 768w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/12\/Magento-1x-Writing-Customer-To-Quote-In-Database-600x600.jpg 600w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/12\/Magento-1x-Writing-Customer-To-Quote-In-Database.jpg 944w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption>How a customer is added to a quote on the sales_flat_quote table in the database.<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Unique Findings<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>The customer information (<code>customer_id<\/code>, <code>customer_email<\/code>, <code>customer_firstname<\/code>, etc.) are not written to the quote when the quote is generated<\/li><li>The <code>customer_id<\/code> only is updated on the table every time the quote is saved.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Updating a Quote vs Creating a New Quote<\/h2>\n\n\n\n<p>A new customer would obviously create a new quote record in the&nbsp;<code>sales_flat_quote<\/code>&nbsp;table as there wouldn&#8217;t be a record for that customer.<\/p>\n\n\n\n<p>Additionally, after a quote is converted to an order (after checkout success) the quote has a column&nbsp;<code>is_active<\/code>&nbsp;that is modified from 1 (active) to 0 (inactive\/converted to an order). &nbsp;At this time, it&#8217;s as if the customer is new again, and a new quote will be added to the&nbsp;<code>sales_flat_quote<\/code>&nbsp;table with the next add to cart action.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">When the Customer Information Is Added to the Quote<\/h2>\n\n\n\n<p>The default Magento add to cart method, <code>Mage_Checkout_CartController -&gt; addAction<\/code> writes the quote to <code>sales_flat_quote<\/code>. &nbsp;What I find interesting though is that Magento does not set the customer information (<code>customer_id<\/code>, <code>customer_email<\/code>, etc) on this record in <code>sales_flat_quote<\/code> at this time. Those values are set to <code>NULL<\/code>. &nbsp;If the customer has an active quote in the table already, the table will of course not have&nbsp;<code>NULL<\/code>&nbsp;for the customer information fields as they were already set. &nbsp;The customer information fields will however be set to the table the next time that the quote is saved.<\/p>\n\n\n\n<p>The default Magento add to cart method redirects&nbsp;the customer to the cart page (<code>www.yourdomain.com\/checkout\/cart\/<\/code>) after the add to cart process is complete. &nbsp;At this time, the cart is updated and thus the customer information will be set, obtaining this information from&nbsp;the <code>checkout\/session<\/code> singleton.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Walk Through the Core Code<\/h2>\n\n\n\n<p>The cart controller, <code>Mage_Checkout_CartController -&gt; indexAction<\/code> then loads the quote. This action calls:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">$cart = $this-&gt;_getCart();\n\/**\n * Retrieve shopping cart model object\n *\n * @return Mage_Checkout_Model_Cart\n *\/\nprotected function _getCart()\n{\n    return Mage::getSingleton('checkout\/cart');\n}<\/pre>\n\n\n\n<p>This gets the cart from the singleton <code>Mage_Checkout_Model_Cart<\/code>. The <code>init()<\/code> method calls the <code>getQuote()<\/code>method:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">\/**\n * Initialize cart quote state to be able use it on cart page\n *\n * @return Mage_Checkout_Model_Cart\n *\/\npublic function init()\n{\n    $quote = $this-&gt;getQuote()-&gt;setCheckoutMethod('');\n    .....\n    return $this;\n}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">\/**\n * Get quote object associated with cart. By default it is current customer session quote\n *\n * @return Mage_Sales_Model_Quote\n *\/\npublic function getQuote()\n{\n    if (!$this-&gt;hasData('quote')) {\n        $this-&gt;setData('quote', $this-&gt;getCheckoutSession()-&gt;getQuote());\n    }\n    return $this-&gt;_getData('quote');\n}<\/pre>\n\n\n\n<p>This then calls the <code>getCheckoutSession()<\/code> method which loads the customer information from the <code>checkout\/session<\/code> singleton.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">\/**\n * Retrieve checkout session model\n *\n * @return Mage_Checkout_Model_Session\n *\/\npublic function getCheckoutSession()\n{\n    return Mage::getSingleton('checkout\/session');\n}<\/pre>\n\n\n\n<p>That whole chain of commands finally sets the customer information to the quote object <code>$cart<\/code> in the cart controller, <code>Mage_Checkout_CartController -&gt; indexAction<\/code> and eventually gets saved to the <code>sales_flat_quote<\/code> table when the <code>Mage_Checkout_CartController -&gt; indexAction<\/code> calls:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$cart-&gt;save();<\/pre>\n\n\n\n<p>That long explanation shows how the customer information finally makes it to the <code>sales_flat_quote<\/code> table when the default Magento method is used.<\/p>\n\n\n\n<p>There is one additional piece of complexity to this process. Every time a quote is saved (add to cart, view cart page, every step in the checkout process) the <code>customer_id<\/code> only will be updated by <code>Mage_Sales_Model_Quote -&gt; _beforeSave()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">\/**\n * Prepare data before save\n *\n * @return Mage_Sales_Model_Quote\n *\/\nprotected function _beforeSave()\n{\n    ....\n    if ($this-&gt;_customer) {\n        $this-&gt;setCustomerId($this-&gt;_customer-&gt;getId());\n    }\n    parent::_beforeSave();\n}<\/pre>\n\n\n\n<p>Looking closer at how the quote is loaded,&nbsp;<code>Mage_Checkout_Model_Cart -&gt; getQuote()<\/code> calls:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">$this-&gt;getCheckoutSession()-&gt;getQuote()\n<\/pre>\n\n\n\n<p>The <code>getCheckoutSession<\/code> method loads from the <code>checkout\/session<\/code> singleton:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted lang:php decode:true\">\/**\n * Retrieve checkout session model\n *\n * @return Mage_Checkout_Model_Session\n *\/\npublic function getCheckoutSession()\n{\n    return Mage::getSingleton('checkout\/session');\n} <\/pre>","protected":false},"excerpt":{"rendered":"<p>I was trying to troubleshoot an issue with quotes in Magento 1.x the other day and did some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1440,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[46,5],"tags":[],"class_list":["post-1434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2016\/12\/Magento-1x-Writing-Customer-To-Quote-In-Database.jpg","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-n8","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=1434"}],"version-history":[{"count":5,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1434\/revisions"}],"predecessor-version":[{"id":2025,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/1434\/revisions\/2025"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/1440"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=1434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=1434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=1434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}