Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Add Order Attribute Programmatically in Magento 2?

The more data you have, the better you understand your customers and the more operations you can perform. This is the reason store owners want to collect as much data from customers as possible. Custom order Attributes, customer attributes, and product attributes are created for this very purpose. They are the most popular operations in any ecommerce store.

We have already written on creating customer attributes and product attributes Programmatically in Magento 2. Here, we will see how to add order custom order attributes programmatically.

Steps to Add Order Attribute Programmatically in Magento 2

The following steps will do.

Step 1: Create an UpgradeData File

File Path: app/code/Company/Mymodule/Setup/UpgradeData.php

salesSetupFactory = $salesSetupFactory;
 }

 public function upgrade(

 ModuleDataSetupInterface $setup,

 ModuleContextInterface $context

 ) {

 if (version_compare($context->getVersion(), "1.0.1", "salesSetupFactory->create(['setup' => $setup]);

 $salesSetup->addAttribute(

 'order',

 'custom_order_attribute',

 [

 'type' => 'varchar',

 'length' => 5,

 'visible' => false,

 'required' => false,

 'grid' => true

 ]
 );
 }
 }
}

This will create the custom order attribute ‘custom_order_attribute’ in the sales_order table and sales_order_grid table.

Step 2: Save the Order Attribute Using Observer

Next, you want to set value for this attribute. We will use sales_order_save_after event to set value when the order is created. So we will create new events.xml file in Company/Mymodule/etc/events/xml with the following content.

And the actual code for this will be in

Company\Mymodule\Observer\Sales\SetOrderAttribute.php with the following content.

logger = $logger;
 $this->productRepository = $productRepository;
 }


 public function execute(
 \Magento\Framework\Event\Observer $observer
 ) {
 $order= $observer->getData('order');
 $order->setCustomAttribute("Yes"); 
 $order->save();
 }
}

We set the value of the custom order attribute and saved the order object.

Step 3: Sync sales_order table and sales_order_grid table

Now, we need to tell Magento to copy values from sales_order to sales_order_grid table when a new order is created. That is done by di.xml file. Create di.xml in Company/Mymodule/etc/di.xml with the following content.

sales_order.custom_order_attribute

Step 4: Show Custom Order Attribute in Grid

Nex, we need to tell the sales_order_grid.xml UI component to create a column for our new attribute. Create a new xml file in Company/Mymodule/view/adminhtml/ui_component/sales_order_grid.xml with the following content.

textCustom Order Attribute

Now you will see the column Custom Order Attribute in the admin grid with value Yes.

If you have any issue in adding order attribute programmatically in Magento 2, then feel free to contact our support team for a quick fix.

Related Articles:

  • Magento 2 Create Attribute Set Programmatically
  • How to Add Custom Checkout Fields in Magento 2?
  • Magento 2 Add Custom Fields to Shipping Address Programmatically
  • Add Custom Fields to Magento 2 Shipping Method


This post first appeared on Magento Vs PrestaShop Themes–Who Is Best? / Mag, please read the originial post: here

Share the post

How to Add Order Attribute Programmatically in Magento 2?

×

Subscribe to Magento Vs Prestashop Themes–who Is Best? / Mag

Get updates delivered right to your inbox!

Thank you for your subscription

×