Magento – save order attribute

attributesmagento2sales-order

I created an order attribute using quoteSetupFactory or eavSetupFactory which yields in creation of a row in the eav_attribute table with entity_type_id of 5.

$quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);
$quoteInstaller->addAttribute(
        'order',
        'me_attrib',
        ['type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER]
    );

when I try to set the value for that attribute using orderResource an sql error complains that the column doesn't exist.

$order->setData("me_attrib","1");
$this->orderResource->saveAttribute($order, ['me_attrib']);

**error**  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'me_attrib' in 'field list', query was: UPDATE `sales_order` SET ` `me_attrib` = ? WHERE (entity_id= 2)

So apparently I have to create a new column on sales_order table in order to store the new attribute.

I was wondering if there is any way to use the eav_attribute instead of creating a new column?

Best Answer

Quotes and orders do not implement EAV storage, so no, you will not be able to use EAV attributes for those models. You have to add your table or column manually, and you should handle the data via ExtensionAttributes.

The only EAV storage data types (out of box) are customer, address, category, and product. If you look at those in the database, you'll notice each one has a primary table (usually named *_entity), followed by several storage tables for different data types: *_entity_text, *_entity_varchar, *_entity_int, etc. No such tables exist for sales_order.

Related Topic