Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a number of steps required here to get the whole solution. </p> <p>Firstly, create a new module. Use the <a href="http://www.magentocommerce.com/extension/1108/modulecreator" rel="noreferrer">ModuleCreator</a> if you wish. </p> <p>Then, write a <a href="http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-6-magento-setup-resources" rel="noreferrer">setup script</a> in your module to add the new fields to Magento's attribute structure, e.g. :</p> <pre><code> $setup = new Mage_Sales_Model_Mysql4_Setup('core_setup'); $setup-&gt;startSetup(); $setup-&gt;addAttribute('quote', 'my_attribute', array('type' =&gt; 'varchar', 'visible' =&gt; false, 'required' =&gt; false)); $setup-&gt;addAttribute('order', 'my_attribute', array('type' =&gt; 'varchar', 'visible' =&gt; false, 'required' =&gt; false)); $setup-&gt;addAttribute('invoice', 'my_attribute', array('type' =&gt; 'varchar', 'visible' =&gt; false, 'required' =&gt; false)); $setup-&gt;addAttribute('creditmemo', 'my_attribute', array('type' =&gt; 'varchar', 'visible' =&gt; false, 'required' =&gt; false)); </code></pre> <p>Note the use of the <code>Mage_Sales_Model_Mysql4_Setup</code> to add the fields to the relevant <code>sales_flat_quote</code> and <code>sales_flat_order</code> tables.</p> <p>Now, insert the following values in your module's config.xml file:</p> <pre><code>&lt;global&gt; &lt;fieldsets&gt; &lt;sales_convert_quote&gt; &lt;my_attribute&gt; &lt;to_order&gt;*&lt;/to_order&gt; &lt;/my_attribute&gt; &lt;/sales_convert_quote&gt; &lt;sales_convert_order&gt; &lt;my_attribute&gt; &lt;to_cm&gt;*&lt;/to_cm&gt; &lt;to_invoice&gt;*&lt;/to_invoice&gt; &lt;/my_attribute&gt; &lt;/sales_convert_order&gt; &lt;/fieldsets&gt; </code></pre> <p>That will instruct Magento to copy the values of your custom field from quote to order to invoice and credit_memo, etc. </p> <p>Then in your custom block/controller code, you will be able to use Magento's magic getters and setters to persist the values. </p> <pre><code>$oQuote = Mage::getSingleton('checkout/session')-&gt;getQuote(); $oQuote-&gt;setMyAttribute('some_value'); $oQuote-&gt;save(); </code></pre> <p>You should see the new column and value saved in <code>sales_flat_quote</code>. Then once the customer completes checkout, the same value should be saved in <code>sales_flat_order</code>. </p> <p>Note that the above code can be extended to work for <code>quote_item</code> and <code>order_item</code> by just changing <code>quote</code> to <code>quote_item</code> etc, however, if you wish to save attribute values that have been set on your products, then some extra work is required. </p> <p>Insert a new block of XML into your config.xml (again inside the global node):</p> <pre><code> &lt;sales&gt; &lt;quote&gt; &lt;item&gt; &lt;product_attributes&gt; &lt;my_attribute /&gt; &lt;/product_attributes&gt; &lt;/item&gt; &lt;/quote&gt; &lt;/sales&gt; </code></pre> <p>Where <code>my_attribute</code> is the attribute code on the product model. That will make the my_attribute available on the linked product, so you can access it via </p> <pre><code>$oQuoteItem-&gt;getProduct()-&gt;getMyAttribute() </code></pre> <p>without needing to perform a full <code>Mage::getModel('catalog/product')-&gt;load($oQuoteItem-&gt;getProductId())</code>. This is much more efficient. </p> <p>Then, you will need an observer to copy the values from the product object to the quote_item object. So, declare your observer in the config.xml: </p> <pre><code> &lt;events&gt; &lt;sales_quote_item_set_product&gt; &lt;observers&gt; &lt;quoteitem_set_custom_data&gt; &lt;type&gt;singleton&lt;/type&gt; &lt;class&gt;mymodule/observer&lt;/class&gt; &lt;method&gt;setCustomDataOnQuoteItem&lt;/method&gt; &lt;/quoteitem_set_custom_data&gt; &lt;/observers&gt; &lt;/sales_quote_item_set_product&gt; &lt;events&gt; </code></pre> <p>and write code in your observer class like this:</p> <pre><code>public function setCustomDataOnQuoteItem($oObserver){ $oProduct = $oObserver-&gt;getProduct(); $oQuoteItem = $oObserver-&gt;getQuoteItem(); foreach(array('my_attribute') as $vAttributeCode){ $oQuoteItem-&gt;setData($vAttributeCode,$oProduct-&gt;getData($vAttributeCode)); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload