0

I would like to add two custom field for order process.

My config file:

    <resources>
        <sales_custom_attribute>
            <setup>
                <module>Ucs_CatalogOrder</module>
                <class>Mage_Sales_Model_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </sales_custom_attribute>
    </resources>

Below is MySQl:

Location: app\code\local\Ucs\CatalogOrder\sql\sales_custom_attribute\mysql4-install-1.0.0.php

<?php
$installer = $this;
$installer->startSetup();

$this->addAttribute('quote_item', 'card_wallet_amount', array(
        'type' => 'varchar',
        'input' => 'text',
        'label' => 'Card handling Charges',
        'global' => 1,
        'visible' => 1,
        'required' => 0,
        'user_defined' => 1,
        'visible_on_front' => 0
    ));
$this->addAttribute('order_item', 'card_wallet_amount', array(
        'type' => 'varchar',
        'input' => 'text',
        'label' => 'Card handling Charges',
        'global' => 1,
        'visible' => 1,
        'required' => 0,
        'user_defined' => 1,
        'visible_on_front' => 0
    ));

$installer->endSetup();

After clearing cache I reload my page. When I check core_resource I can see the module sql record. But the fields haven't been added.

1 Answer 1

2

In older Magento quote and order are using EAV attributes but from Magento version 1.6 orders does not have attributes anymore.

So instead of attribute you will need to create fields in sales_flat_quote_item and sales_flat_order_item tables.

For that you will need to use alter table script in your sql setup file.

$installer->run("ALTER TABLE `{$installer->getTable('sales/quote_item')}` ADD `card_wallet_amount` varchar(255) DEFAULT NULL;");
$installer->run("ALTER TABLE `{$installer->getTable('sales/order_item')}` ADD `card_wallet_amount` varchar(255) DEFAULT NULL;");

And you will need to save your value in quote. In your module config.xml file you will need to convert your quote field to order. You can do like

<global>
  <fieldsets>
       <sales_convert_quote_item>
           <card_wallet_amount>
               <to_order_item>*</to_order_item>
           </card_wallet_amount>
       </sales_convert_quote_item>
   </fieldsets>
</global>
6
  • Thanks for your reply. I am clear till the $installer->run("ALTER TABLE ... part. Whats the second part? Should it be required?
    – Anto S
    Oct 12, 2015 at 6:27
  • 1
    yes, it is require. I think you are talking about xml part. once you save your value to sales_flat_quote_item table and place your order. system will automatically copy this field value from sales_flat_quote_item to sales_flat_order_item table. Oct 12, 2015 at 7:30
  • Thanks mate. I will try it once and accept. +1 now. Thank you so much
    – Anto S
    Oct 12, 2015 at 10:36
  • But when I get order details using $order = Mage::getModel('sales/order')->load($orderId); these custom values are not getting displayed?
    – Anto S
    Oct 12, 2015 at 13:40
  • you will need to get order items from this order object. you can get it my $ordered_items = $order->getAllItems(); also make sure in your order item table your custom field value saved correctly after placing an order. Oct 12, 2015 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.