Sylwester Kardziejonek
Sylwester Kardziejonek

Reputation: 592

Magento 1.6.2.0 sales orders custom attribute is not working

I already have spent all day on this and I think I can't get this working because the possibility to add custom EAV attributes to orders was removed. At least, I've noticed that sales_order_entity is missing.

Well, what I try to do is to add a custom field to sales orders. I thought it would work the same way as for category products, but looks like nope. My overall point in doing all this is because I want to track who is adding products to catalog and want to relate certain order with certain user (not customer).

public function getDefaultEntities()
{
    return array(
        'catalog_product' => array(
            'entity_model'      => 'catalog/product',
            'attribute_model'   => 'catalog/resource_eav_attribute',
            'table'             => 'catalog/product',
            'additional_attribute_table' => 'catalog/eav_attribute',
            'entity_attribute_collection' => 'catalog/product_attribute_collection',
            'attributes'        => array(
                'seller_id' => array(
                    'group'             => 'MyCustom',
                    'label'             => 'Seller ID',
                    'type'              => 'int',
                    'input'             => 'text',
                    'default'           => '0',
                    'class'             => '',
                    'backend'           => '',
                    'frontend'          => '',
                    'source'            => '',
                    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                    'visible'           => false,
                    'required'          => true,
                    'user_defined'      => true,
                    'searchable'        => true,
                    'filterable'        => true,
                    'comparable'        => false,
                    'visible_on_front'  => false,
                    'visible_in_advanced_search' => false,
                    'unique'            => false,
                ),
            ),
        ),
        'order' => array(
            'entity_model'      => 'sales/order',
            'table'             => 'sales/order',
            'increment_model'   => 'eav/entity_increment_numeric',
            'attributes'        => array(
                'seller_id' => array(
                    'group'             => 'MyCustom',
                    'label'             => 'Seller ID',
                    'type'              => 'int',
                    'input'             => 'text',
                    'default'           => '0',
                    'class'             => '',
                    'backend'           => '',
                    'frontend'          => '',
                    'source'            => '',
                    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                    'visible'           => false,
                    'required'          => true,
                    'user_defined'      => true,
                    'searchable'        => true,
                    'filterable'        => true,
                    'comparable'        => false,
                    'visible_on_front'  => false,
                    'visible_in_advanced_search' => false,
                    'unique'            => false,
                ),
            ),
        ),
    );
}

It works for products, but not for orders. I have required entries in eav_attribute table.

I don't know if I'm doing something wrong or this is just impossible to do? I also thought about solving this different way by creating additional table to track relations between user - order|product. This would require more work tough.

Upvotes: 1

Views: 1307

Answers (2)

Son Cao
Son Cao

Reputation: 21

For Versions > 1.4.1.0 you will have to create a column in the sales_flat_order table. You can take a look in this post is magento sales eav

Upvotes: 0

pcreact
pcreact

Reputation: 41

I know this is an old post but I came across it when I was looking for answers to the same problem, so thought I would share my solution.

Lets take adding an attribute to a sales order as an example.

Firstly, Magento no longer users EAV entities for Sales, if you look at core/Mage/Sales/etc/config.xml

<sales>
      <class>Mage_Sales_Model</class>
      <resourceModel>sales_resource</resourceModel>
</sales>

resouceModel no longer points at sales_entity(EAV), it now points at sales_resource(flat). If you look at the children of the sales_resource node, you will find the order node:

<order>
      <table>sales_flat_order</table>
</order>

This means that a Mage_Sales_Model_Order has a resource model of Mage_Sales_Model_Resource_Order which has a table of sales_flat_order.

The magento developers have provided the class Mage_Sales_Model_Resource_Setup which allows you to add attributes to this new "flat" structure in pretty much the same way you would have added attributes to the EAV structure. If you take a look inside Mage_Sales_Model_Resource_Setup you will see the following function:

/**
 * Add entity attribute. Overwrited for flat entities support
 *
 * @param int|string $entityTypeId
 * @param string $code
 * @param array $attr
 * @return Mage_Sales_Model_Resource_Setup
 */
public function addAttribute($entityTypeId, $code, array $attr)
{

    if (isset($this->_flatEntityTables[$entityTypeId]) &&
        $this->_flatTableExist($this->_flatEntityTables[$entityTypeId]))
    {
        $this->_addFlatAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr);
        $this->_addGridAttribute($this->_flatEntityTables[$entityTypeId], $code, $attr, $entityTypeId);
    } else {
        parent::addAttribute($entityTypeId, $code, $attr);
    }
    return $this;
}

With this information you should now see that it is simply a case of getting an instance of Mage_Sales_Model_Resource_Setup and calling its public addAttribute method with valid parameters.

Code snippet of adding a franchise_id attribute to a sales order:

$salesResourceSetupModel = Mage::getModel('sales/resource_setup', 'core_setup');

$data=array(
    'type'=>'int',
    'input'=>'text',
    'label'=>'Franchise',
    'global'=> 1,
    'is_required'=>'0',
    'is_comparable'=>'0',
    'is_searchable'=>'0',
    'is_unique'=>'0',
    'is_configurable'=>'0',
    'user_defined'=>'1',
    //whether it should be including in the sales order grid
    'grid'=>1
);

//first param should relate to a key of the protected $_flatEntityTables array
$salesResourceSetupModel->addAttribute('order', 'franchise_id', $data);

Upvotes: 4

Related Questions