4

I've created a custom order attribute in my extension's setup script like this:

$setup->addAttribute('order', 'my_custom_attribute', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'My Custom Attribute',
    'global' => 0,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'default' => '',
    'visible_on_front' => 0,
));

When I look into eav_attribute table in the DB after running this script I can see that a new entry has been created with the options listed above and assigned to the entity_type_id of 4, which is the sales/order entity model in this installation.

Now when I do the following, I get an empty result:

$order = Mage::getModel('sales/order')->load(90);
$order->setData('my_custom_attribute', 'test');
$order->save();

$order = Mage::getModel('sales/order')->load(90);
echo $order->getData('my_custom_attribute'); // NULL

Is there a step that I'm missing here?

2 Answers 2

4

You are probably using wrong class during setup. You need to make sure that $setup is instance of Mage_Sales_Model_Resource_Setup class. I'm not sure but you are probably using Mage_Eav_Model_Entity_Setup.

If there is no my_custom_attribute row in sales_flat_order table you are probably using wrong setup class. You can also remove my_custom_attribute from eav_attribute table.

3
  • Thanks! That seems to have been one issue with my code. However I'm still unable to write to the custom attribute: Mage::getModel('sales/order')->load(90)->setData('my_custom_attribute', 'something')->save(). Do you perhaps know why this doesn't work?
    – Louis B.
    Commented Jan 30, 2015 at 20:02
  • 1) Verify that my_custom_attribute column exists in sales_flat_order 2) Try flushing cache Commented Jan 30, 2015 at 20:08
  • Ah, flushed the cachees and reindexed all tables, not it's working. Thanks!
    – Louis B.
    Commented Jan 30, 2015 at 20:13
1

Your code works fine for me. If you used that exact code then you're missing a quote after attribute in your attribute name.

$setup->addAttribute('order', 'my_custom_attribute, array(

The only other thing would be to make sure the order ID exists.

Here is the full install script I used to install your attribute:

<?php

require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$installer = new Mage_Sales_Model_Mysql4_Setup;
$installer->addAttribute('order', 'my_custom_attribute', array(
    'type' => 'varchar',
    'input' => 'text',
    'label' => 'My Custom Attribute',
    'global' => 0,
    'visible' => 1,
    'required' => 0,
    'user_defined' => 1,
    'default' => '',
    'visible_on_front' => 0,
));

$installer->endSetup();
4
  • Thanks for testing my code. I changed the error with the wrong setup class, and now I can see that a column for my_custom_attribute has been added to the sales_flat_order table. However, I'm still unable to write anything to that column like this: Mage::getModel('sales/order')->load(90)->setData('my_custom_attribute', 'something')->save(). Did that work for you?
    – Louis B.
    Commented Jan 30, 2015 at 20:01
  • 1
    Yes that part worked fine for me. I was able to save and retrieve. Just make sure entity_id 90 exists. Commented Jan 30, 2015 at 20:06
  • Ah, flushed the cachees and reindexed all tables, not it's working. Thanks! I have to however accept the other guy's answer as he was two minutes earlier.
    – Louis B.
    Commented Jan 30, 2015 at 20:14
  • No worries! I'm just glad you got it resolved Commented Jan 30, 2015 at 21:19

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.