How to add order attribute programmatically in Magento 2?

The Magento 2 add custom attribute to order feature is critical for collecting customer data that informs your future selling strategies. Understanding the Magento 2 add order attribute programmatically option is important for getting the most from your custom attributes and data.

But how do you programmatically add an order attribute in Magento 2? Read more to find out.

Step One: Create the UpgradeData File

The first step for the Magento 2 add custom order attribute process is creating an UpgradeData file. The file path for this step will be app/code/Company/Mymodule/Setup/UpgradeData.php.

You’ll see this code:

salesSetupFactory = $salesSetupFactory;
}
public function upgrade(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
if (version_compare($context->getVersion(), "1.0.1", "<")) {
$salesSetup = $this->salesSetupFactory->create(['setup' => $setup]);
$salesSetup->addAttribute(
'order',
'custom_order_attribute',
[
'type' => 'varchar',
'length' => 5,
'visible' => false,
'required' => false,
'grid' => true
]
);
}
}
}

This step creates the custom order attribute custom_order_attribute in your sales_order and sales_order_grid tables.

Step Two: Saving the Order Attribute

Next, you’ll want to save your Magento 2 custom order attribute. Use the sales_order_save_after event to set a value for when an order is created.

You must to create new events.xml files under Company/Mymodule/etc/events/xml. The code for this command will be found in CompanyMymoduleObserverSalesSetOrderAttribute.php. You’ll see the following displayed:

logger = $logger;
$this->productRepository = $productRepository;
}
public function execute(
MagentoFrameworkEventObserver $observer
) {
$order= $observer->getData('order');
$order->setCustomAttribute("Yes");
$order->save();
}
}

This step will allow you to set the specific value of a custom order attribute and save your order object.

Step Three: Syncing

After saving the order attribute, you must sync the sales_order table and the sales_order_grid table. You can accomplish this step using the di.xml file created in Company/Mymodule/etc/di.xml.

Use sales_order.custom_order_attribute to complete this step.

Step Four: Showing the Custom Order Attribute in Grid

The final step of adding an order attribute programmatically in Magento 2 is showing the custom order attribute in your grid. This step is possible by telling the sales_order_grid.xml UI component to create a column for your custom order attribute.

You can develop a new xml file in Company/Mymodule/view/adminhtml/ui_component/sales_order_grid.xml using the text Custom Order Attribute content.

After this, you’ll have access to the Custom Order Attribtue option in the admin grid with the value Yes.

Add Custom Order Attributes With Amasty

Navigating the Magento 2 add order attribute process programmatically can be challenging if you aren’t familiar with coding. Amasty provides a Magento 2 Order Attributes extension to help you add custom order attributes effortlessly.

This extension allows you to add unlimited fields to your product checkout page, making it easy to collect and process data. Amasty helps you display all created order attributes on a backend grid to ensure you have the information you need to succeed.

How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.

© 2009-2024 Amasty. All Rights Reserved.