To get the whole solution, you need to follow a few steps.
First, create a new module. Use ModuleCreator if you want.
Then write setup script in your module to add new fields to the Magento attribute structure, for example.
$setup = new Mage_Sales_Model_Mysql4_Setup('core_setup'); $setup->startSetup(); $setup->addAttribute('quote', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false)); $setup->addAttribute('order', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false)); $setup->addAttribute('invoice', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false)); $setup->addAttribute('creditmemo', 'my_attribute', array('type' => 'varchar', 'visible' => false, 'required' => false));
Note the use of Mage_Sales_Model_Mysql4_Setup
to add fields to the respective sales_flat_quote
and sales_flat_order
.
Now add the following values ββto the config.xml file of your module:
<global> <fieldsets> <sales_convert_quote> <my_attribute> <to_order>*</to_order> </my_attribute> </sales_convert_quote> <sales_convert_order> <my_attribute> <to_cm>*</to_cm> <to_invoice>*</to_invoice> </my_attribute> </sales_convert_order> </fieldsets>
This will instruct Magento to copy the values ββof your custom field from the billing quote and credit_memo, etc.
Then in your custom block / controller code, you can use Magento magic getters and seters to save the values.
$oQuote = Mage::getSingleton('checkout/session')->getQuote(); $oQuote->setMyAttribute('some_value'); $oQuote->save();
You should see a new column and value stored in sales_flat_quote
. Then, as soon as the client completes the check, the same value should be stored in sales_flat_order
.
Please note that the above code can be extended to work quote_item
and order_item
, just changing the quote
to quote_item
, etc., however, if you want to save the attribute values ββset on your products, then additional work is required.
Insert a new XML block into your config.xml (again inside the global node):
<sales> <quote> <item> <product_attributes> <my_attribute /> </product_attributes> </item> </quote> </sales>
Where my_attribute
is the attribute code in the product model. This will make my_attribute available to the related product so that you can access it through
$oQuoteItem->getProduct()->getMyAttribute()
without doing the full Mage::getModel('catalog/product')->load($oQuoteItem->getProductId())
. It is much more efficient.
Then you need an observer to copy the values ββfrom the product object to the quote_item object. So declare your observer in config.xml:
<events> <sales_quote_item_set_product> <observers> <quoteitem_set_custom_data> <type>singleton</type> <class>mymodule/observer</class> <method>setCustomDataOnQuoteItem</method> </quoteitem_set_custom_data> </observers> </sales_quote_item_set_product> <events>
and write the code in your observer class as follows:
public function setCustomDataOnQuoteItem($oObserver){ $oProduct = $oObserver->getProduct(); $oQuoteItem = $oObserver->getQuoteItem(); foreach(array('my_attribute') as $vAttributeCode){ $oQuoteItem->setData($vAttributeCode,$oProduct->getData($vAttributeCode)); } }