views:

440

answers:

1

Hi,

I'm trying to add custom fields to magento checkout onepage. I followed an example that doesn't work in 1.4.1 because of the move to a flat order table(I think) http://inchoo.net/ecommerce/magento/adding-a-new-tab-under-one-page-checkout-full-working-module/

I can see my custom tab in checkout page with my custom fields but I can't save the fields to the database.

  • How do I add columns to the quote and order tables? should this go to Mymod/sql/mymod_setup/mysql4-install-0.1.0.php or somewhere else?

  • How do I save the field to the db? Do I need to save it to the quote first? Do I use observer or something else? Do I need to have element in my module's config.xml? http://www.magentocommerce.com/boards/viewthread/19344/

Thanks

A: 

Disclaimer: I have not touched Magento for 6 months. Now this is said, if you look in the app/code/core/Mage/Sales/sql/sales_setup/ directory you will find examples of how to modify order tables. For instance here is the content of app/code/core/Mage/Sales/sql/sales_setup/mysql4-upgrade-0.9.12-0.9.13.php (without header comments):

$installer = $this;
/* @var $installer Mage_Sales_Model_Mysql4_Setup */

$installer->addAttribute('quote', 'subtotal', array('type'=>'decimal'));
$installer->addAttribute('quote', 'base_subtotal', array('type'=>'decimal'));

$installer->addAttribute('quote', 'subtotal_with_discount', array('type'=>'decimal'));
$installer->addAttribute('quote', 'base_subtotal_with_discount', array('type'=>'decimal'));

$this is initialized from what is found in the app/code/core/Mage/Sales/etc/config.xml following the config/global/resources/sales_setup/setup/class look into this class and you'll see it inherits from Mage_Eav_Model_Entity_Setup, the default setup class, and overrides or add some methods (for flat table support for instance).

To answer your first point (first question), you can add columns by using the addAttribute() method of this class. And the answer to the second question is yes, but you have to specify in the config.xml file of your module that you want to use Mage_Sales_Model_Mysql4_Setup as setup class. This is done by adding the same xml element found in app/code/core/Mage/Sales/etc/config.xml previously (replace sales_setup with yourmod_setup). So you dump your database, you check it works by using get_class($this) in your mysql4-install-0.1.0.php file, and then you restore your db. And you continue writing in your setup file, inspiring yourself from what you see in the files in app/code/core/Mage/Sales/sql/sales_setup and it should be fine! Now for the second point... I don't know... I hope it'll work automatically! Good luck!

greg0ire
I don't think it works for 1.4.1 because the table changed from EAV to flat. Look at mysql4-upgrade-1.4.0.2-1.4.0.3.php. They use addColumn instead of addAttribute.
pablo
@pablo: did you read my answer? if says some methods where added for flat table support. It is the case of addAttribute() method. Just have a look to the Mage_Sales_Model_Mysql4_Setup class.
greg0ire
I missed the fact that you are suggesting to use the Sales setup module. I'll try it and report my results. Thanks.
pablo
In the same module I need to add attributes to sales profile and create a custom flat table so I'm using: $setup = new Mage_Sales_Model_Mysql4_Setup('sales_setup'); for sales and $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); for profile.Thanks
pablo