Magento 2 Create Order Attribute Programmatically

How to Create Order Attribute Programmatically in Magento 2

by nkhuyen

Magento 2 order attributes are essential for gathering customer information. However, Magento store owners need to establish a custom order attribute in Magento 2 and display it in the admin grid to enhance order processing efficiency as your business grows.

You can refer to the steps outlined in Magento 2 create order attribute programmatically and show it in the admin grid in this blog post. We’ll break down 4 steps and recommend some Magento extensions to streamline creating order attributes in Magento 2 that you can consider.

Overview of an Order Attribute in Magento 2

In Magento 2, Order Attributes are extra fields that can be added to the checkout page. These attributes allow store owners to collect more information from customers during the checkout process. The collected data helps in managing and processing orders and makes the overall checkout experience better. Store owners can customize these attributes in different formats like Text Field, Text Area, Date, Yes/No, Dropdown, Multiple Select and Single Select with Image.

how to create an order attribute programmatically in Magento 2

In addition, order attributes can be shown on the checkout page, in PDF invoices, and in transactional emails. They can also be dependent on other attributes like selected shipping method. Magento store admin can add order attributes in the Sales Order Grid in the backend, get a detailed view of order information, and update and track orders efficiently.

To learn more about order attributes in default Magento 2 and why this feature is important to your online store, let’s explore this blog post: Why Advanced Order Attributes Will Be Your Biggest Magento Success

Magento 2 Create Order Attribute Programmatically: 4 Steps

You need to be able to add custom attributes to orders in Magento 2 to gather customer data that will inform your future sales. Understanding the programmatic way of adding order attributes in Magento 2 will help you get the most out of your custom attributes and the data that comes with them.

But what is the process of programmatically adding an order attribute in Magento 2? This guide will walk you through a 4-step process to create a custom order attribute in Magento 2. By the end of this tutorial, you will be able to add new fields to your order forms and use the data collected for your business purposes.

Let’s dive in!

Step 1. Build an UpgradeData File

The first phase of the process to add a custom order attribute in Magento 2 involves the creation of an UpgradeData file. The designated file path for this action is app/code/Company/Mymodule/Setup/UpgradeData.php.

The following code will be observed:

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 phase creates the custom order attribute named custom_order_attribute within the sales_order and sales_order_grid tables.

Step 2. Save the Order Attribute Using Observer

The next step is saving your custom order attribute in Magento 2. Use the sales_order_save_after event to assign a value at the moment an order is created.

You are required to establish new events.xml files within the Company/Mymodule/etc/events/xml directory. The corresponding code for this operation will be located in Company/Mymodule/Observer/SalesSetOrderAttribute.php. The following information will be presented:

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

This action will enable you to define the precise value of a custom order attribute and subsequently save your order object.

Step 3. Sync sales_order table and sales_order_grid table

After saving the order attribute, it is essential to synchronize both the sales_order table and the sales_order_grid table. This can be achieved by utilizing the di.xml file located in Company/Mymodule/etc/di.xml. To finalize this process, use sales_order.custom_order_attribute.

Step 4. Displaying the Custom Order Attribute in Grid

The final phase of programmatically incorporating a custom order attribute in Magento 2 involves displaying this attribute within your grid. This can be achieved by instructing the sales_order_grid.xml UI component to generate a column dedicated to your custom order attribute.

To implement this, you should create a new XML file located at Company/Mymodule/view/adminhtml/ui_component/sales_order_grid.xml, utilizing the content labeled as Custom Order Attribute.

Then, you can view the Custom Order Attribute option in the admin grid, with its value set to Yes.

>>> You may want to know: How to Add Custom Fields in Magento Checkout Page

Magento 2 Add Custom Order Attribute: The Best Solution

Magento 2 is flexible and you can customize everything including custom order attributes. While the admin panel is user-friendly, the programmatic way is more controlled and efficient. But there are multiple ways to do it and choosing the right one can be tricky.

This article delves into the best solution for creating an order attribute in Magento 2. It serves as an order attribute module and supports Magento store owners to confidently implement custom order attributes to enhance your store’s functionality and data management.

A brief overview of BSS Magento 2 Checkout Custom Field, also known as Magento Order Attributes

  • Review: 92% positive
  • Compatible with Magento 2 Checkout Suite
  • Compatible with the Hyva theme
  • Compatible with Hyva Checkout (coming soon)
  • Cost: Only $119 for one-time payment
  • Version update: v1.3.3
  • Support GraphQL API
  • Free installation
  • Free lifetime update
  • Free one year support
  • 30 days money-back guarantee

Magento 2 Checkout Custom Field Extension BSS Commerce

Highlighted Features:

1. Create order attribute fields in Magento 2 to enhance and provide more comprehensive order reports

The functionality to create an unlimited number of fields at checkout allows you to incorporate additional fields for capturing order attributes in your Magento 2 store. This capability not only provides valuable insights into customer preferences but also enhances your understanding of their orders and behaviors.

Furthermore, you can manage this information in a grid format, enabling you to refine and improve a seamless and user-friendly ordering experience.

Highlight Features for Order Attribute 2

2. Add attributes to any stage of the checkout process

Placing new fields in appropriate locations facilitates a more efficient checkout process. There are three areas in the checkout where new attributes can be presented:

  • Shipping Address
  • Shipping Method
  • Review & Payments

Highlight Features for Order Attribute 1

3. Manage custom fields and order attributes at checkout

To make management easier, BSS Commerce have put all new custom fields into a single grid. Also, these custom fields can be stored as order attributes in Order Grid and/or Order Detail Page. This feature allows you to add order attribute fields into Magento 2 order grid, so you can view and filter easily.

Highlight Features for Order Attribute 3

Bonus Tips: On the other hand, using the Magento 2 order management extension by BSS Commerce is a better alternative to creating an order attribute programmatically in the Magento 2 Store. This is an additional software component to improve the order processing and fulfillment feature of an eCommerce platform like Magento 2. Our Magento 2 Order Management extensions cover almost all the features you need to have the most efficient and cost-effective order processing workflow.

Wrapping Up: Magento 2 Add Order Attribute Programmatically

In short, the process of Magento 2 create order attribute programmatically can be a nightmare for those who don’t know how to code. BSS Commerce recommend using a Magento Order Attributes extension to save time and effort. This solution is perfect for creating custom order attributes in Magento 2 and making your store more functional and data manageable.

Looking for a reliable Magento 2 development service? Try BSS service. BSS Commerce provides a wide range of Magento eCommerce development services to fit your business needs.

In a nutshell, BSS Commerce hopes that these suggestions will facilitate a more efficient and expedited installation process. Don’t forget to visit our website frequently to uncover further tips, tricks, and best practices for the effective management of Magento stores.

>>> New to Magento? Read our free beginner’s guide in this post: Magento 2 Basic Tutorial: Everything You Need to Know

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.