cancel
Showing results for 
Search instead for 
Did you mean: 

Order EAV

SOLVED

Order EAV

Hi, I'm a newcomer to Magento.

 

Would like to know if it's possible to add an attribute to 'order' in magento 2? The following is my code for InstallData

 

class InstallData implements InstallDataInterface {
    protected $_eavConfig;
    protected $_salesSetupFactory;

    public function __construct(
            \Magento\Eav\Model\Config $eavConfig,
            \Magento\Sales\Setup\SalesSetupFactory $salesSetupFactory) {
        $this->_eavConfig = $eavConfig;
        $this->_salesSetupFactory = $salesSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        $setup->startSetup();
        
        $eavSetup = $this->_salesSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute('order', 'reward_point', [
            'type' => 'varchar',
            'visible' => false,
            'required' => false
        ]);
        
        $setup->endSetup();
    }
}

After I ran upgrade command, I'm unable to find any new record in my eav_attribute table. I saw here that magento 1.6/1.7 doesn't support eav anymore, does this apply also to Magento 2?

 

If that's scenario, what's the recommended Magento approach if I want to add another field to my order table? Should I add a new column directly to 'sales_order' table or should I create a new table (say 'sales_order_reward' with a foreign key to link to 'order') then left join to retrieve my reward point for the order?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Order EAV

Hi @yat how_kor,

 

As you know Sales module is not EAV, it is flat table in magento.  

Catalog, Customers have EAV module. 

You can directly create an new sales or quote attribute in sales tables. 

You can use following code to create new sales attribute. 

<?php
namespace VP\OrderAttribute\Setup; //CHANGE NAMESPACE AS PER YOUR MODULE

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Sales\Setup\SalesSetupFactory;

class InstallData implements InstallDataInterface
{

    private $salesSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Sales\Setup\SalesSetupFactory $salesSetupFactory
     */
    public function __construct(SalesSetupFactory $salesSetupFactory)
    {
        $this->salesSetupFactory = $salesSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
        $salesSetup->addAttribute('order', 'reward_point',
            [
                'type' => 'varchar',
                'length' => 11,
                'visible' => false,
                'required' => false,
                'grid' => false  // IF you want to show into ORDER Grid then make it true
            ]
        );
    }
}


I hope it will help you!

View solution in original post

1 REPLY 1

Re: Order EAV

Hi @yat how_kor,

 

As you know Sales module is not EAV, it is flat table in magento.  

Catalog, Customers have EAV module. 

You can directly create an new sales or quote attribute in sales tables. 

You can use following code to create new sales attribute. 

<?php
namespace VP\OrderAttribute\Setup; //CHANGE NAMESPACE AS PER YOUR MODULE

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Sales\Setup\SalesSetupFactory;

class InstallData implements InstallDataInterface
{

    private $salesSetupFactory;

    /**
     * Constructor
     *
     * @param \Magento\Sales\Setup\SalesSetupFactory $salesSetupFactory
     */
    public function __construct(SalesSetupFactory $salesSetupFactory)
    {
        $this->salesSetupFactory = $salesSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
        $salesSetup->addAttribute('order', 'reward_point',
            [
                'type' => 'varchar',
                'length' => 11,
                'visible' => false,
                'required' => false,
                'grid' => false  // IF you want to show into ORDER Grid then make it true
            ]
        );
    }
}


I hope it will help you!