Magento – Filtering Order Collection using a custom attribute

attributescollection-filteringeavmagento-1.7orders

I am working on module in which I need to add a custom attribute to the Order Entity but as I tried and failed I found out that Orders Entity is no longer using EAV attributes (or at least EAV table). Is there any way to use custom attribute to filter Orders ?

I am using Magento 1.7.2

Best Answer

Installer (Some/Module/sql/some_module_setup/mysql4-install-0.1.0.php):

/* @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
//add real field to orders table
$installer->getConnection()
    ->addColumn($installer->getTable('sales/order'), 'some_attribute', array(
    'TYPE' => Varien_Db_Ddl_Table::TYPE_INTEGER, //TYPE_VARCHAR,TYPE_DECIMAL,TYPE_DATETIME
    'LENGTH' => 255,
    'NULLABLE' => true,
    'COMMENT' => 'Some Attribute Definition'
));
$installer->endSetup();

Filter Collection:

$collection = Mage::getModel('sales/order')
            ->getCollection()
            ->addFieldToFilter('some_attribute', 'some_value');
Related Topic