We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How to Create Order Attribute Programmatically In Magento 2?
Order attribute enables the store owners and admins to put on the additional fields on the check out page so that the customers can get you aware of the specific demand of the particular orders.
Hence, it is important for the ecommerce business owners to know about the process of creating magento 2 get order
Thus, let’s explore the process
How will you create magento 2 order 2022?
Stage 1: CREATE AN UPGRADEDATA FILE
This blog helps the administrator to gather information of request quality worth which is chosen by the client.
It means quite a bit to collect extra data about clients. As a Magento 2 storekeeper, you can without much of a stretch do it by adding a custom quality to the request. Magento 2 Customer Attributes is the ideal arrangement that can assist you with that.
By utilizing the accompanying advances you can undoubtedly make a custom request trait.
Automatically, you can make Order Attribute Using InstallData File..
Basically, there are two fundamental ways of adding a request trait (another section) to arrange by means of an overhaul script.
1. Utilizing $setup->getConnection()- >addColumn()
app/code/Mageants/SalesOrder/Setup/UpgradeSchema.php
<?php
namespace Mageants\SalesOrder\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* Upgrades DB schema for a module
*
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$quote = 'quote';
$orderTable = 'sales_order';
//Quote table
$setup->getConnection()
->addColumn(
$setup->getTable($quote),
'custom_order_attribute',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'comment' =>'Custom Order Attribute'
]
);
//Order table
$setup->getConnection()
->addColumn(
$setup->getTable($orderTable),
'custom_order_attribute',
[
'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'length' => 255,
'comment' =>'Custom Order Attribute'
]
);
$setup->endSetup();
}
}
2. Utilizing Quote and Sale Setup Factory
app/code/Mageants/SalesOrder/Setup/UpgradeData.php
<?php
namespace Mageants\SalesOrder\Setup;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;
class UpgradeData implements UpgradeDataInterface
{
/**
* @var QuoteSetupFactory
*/
protected $quoteSetupFactory;
/**
* @var SalesSetupFactory
*/
protected $salesSetupFactory;
/**
* @param QuoteSetupFactory $quoteSetupFactory
* @param SalesSetupFactory $salesSetupFactory
*/
public function __construct(
QuoteSetupFactory $quoteSetupFactory,
SalesSetupFactory $salesSetupFactory
) {
$this->quoteSetupFactory = $quoteSetupFactory;
$this->salesSetupFactory = $salesSetupFactory;
}
/**
* Upgrades DB for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var \Magento\Quote\Setup\QuoteSetup $quoteInstaller */
$quoteInstaller = $this->quoteSetupFactory->create(['resourceName' => 'quote_setup', 'setup' => $setup]);
/** @var \Magento\Sales\Setup\SalesSetup $salesInstaller */
$salesInstaller = $this->salesSetupFactory->create(['resourceName' => 'sales_setup', 'setup' => $setup]);
$setup->startSetup();
//Add multiple attributes to quote
$entityAttributesCodes = [
'custom_order_attribute' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
'custom_order_attribute1' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT
];
foreach ($entityAttributesCodes as $code => $type) {
$quoteInstaller->addAttribute('quote', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
$salesInstaller->addAttribute('order', $code, ['type' => $type, 'length'=> 255, 'visible' => false,'nullable' => true, 'grid' => true,]);
$salesInstaller->addAttribute('invoice', $code, ['type' => $type, 'length'=> 255, 'visible' => false, 'nullable' => true,]);
}
$setup->endSetup();
}
}
MAGENTO 2.3.4 AND LATER DO NOT SUPPORT ABOVE FORMAT:
app/code/Mageants/SalesOrder/etc/db_schema.xml
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="sales_order" resource="sales" engine="innodb" comment="Sales Order">
<column xsi:type="varchar" name="custom_order_attribute" nullable="true" length="255" comment="Custom Order Attribute"/>
</table>
<table name="sales_order_grid" resource="sales" engine="innodb" comment="Sales Order">
<column xsi:type="varchar" name="custom_order_attribute" nullable="true" length="255" comment="Custom Order Attribute"/>
</table>
<table name="quote" resource="checkout" engine="innodb" comment="Quote">
<column xsi:type="varchar" name="custom_order_attribute" nullable="true" length="255" comment="Custom Order Attribute"/>
</table>
<table name="sales_invoice" resource="sales" engine="innodb" comment="Sales Invoice">
<column xsi:type="varchar" name="custom_order_attribute" nullable="true" length="255" comment="Custom Order Attribute"/>
</table>
</schema>
This will make the custom request trait 'custom_order_attribute' in the sales_order table, sales_order_grid, Invoice and Quote table.
To make in the sales_order_grid table we are utilizing 'matrix' => valid.
Stage 2: SAVE THE ORDER ATTRIBUTE USING OBSERVER
Then, we want to set incentive for this quality. We will utilize sales_order_save_after occasion to set esteem when the request is made. So we will make new events.xml record in app/code/Mageants/SalesOrder/etc/occasions/xml with the accompanying substance.
app/code/Mageants/SalesOrder/etc/events.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_save_after">
<observer instance="Mageants\SalesOrder\Observer\Sales\OrderSaveAfter" name="set_custom_order_attribute"/>
</event>
</config>
Here we are utilizing sales_order_save_after occasion to refresh the request trait and save it for worth of this quality.
Stage 3: TO CREATE ACTUAL CODE FILE
app/code/Mageants/SalesOrder/Observer/Sales/OrderSaveAfter.php
<?php
namespace Mageants\SalesOrder\Observer\Sales;
class OrderSaveAfter implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var \Magento\Catalog\Model\ProductRepository
*/
protected $productRepository;
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Catalog\Model\ProductRepository $productRepository
) {
$this->logger = $logger;
$this->productRepository = $productRepository;
}
/**
* Execute observer
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function execute(
\Magento\Framework\Event\Observer $observer
) {
$order= $observer->getData('order');
$order->setCustomOrderAttribute("Yes");
$order->save();
}
}
We set the worth of the custom request quality and saved the request object.
Stage 4: SYNC SALES_ORDER TABLE AND SALES_ORDER_GRID TABLE
Presently We need to make di.xml document to duplicate or adjust values from sales_order to sales_order_grid table.
app/code/Mageants/SalesOrder/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
<arguments>
<argument name="columns" xsi:type="array">
<item name="custom_order_attribute" xsi:type="string">sales_order.custom_order_attribute</item>
</argument>
</arguments>
</virtualType>
</config>
Stage 5: SHOW CUSTOM ORDER ATTRIBUTE IN GRID
Then, we want to tell the sales_order_grid.xml UI part to make another segment for our new property. So make a new xml record.
app/code/Mageants/SalesOrder/view/adminhtml/ul_component/sales_order_grid.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="custom_order_attribute">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">textRange</item>
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
<item name="label" xsi:type="string" translate="true">My Order Attribute</item>
</item>
</argument>
</column>
</columns>
</listing>
Conclusion
Thus, this is the process of creating magento 2 order 2022 that is indeed the most beneficial part of today’s ecommerce business. Hope, this blog is useful to you.