How can I add a custom field to admin/sales/order/view?
I'm trying to add a tracking number field to the sales order view.
The tracking number must be saved in the db.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityHow can I add a custom field to admin/sales/order/view?
I'm trying to add a tracking number field to the sales order view.
The tracking number must be saved in the db.
sales_order_grid.xml
<?xml version="1.0"?>
<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="require_verification">
<settings>
<filter>select</filter>
<dataType>select</dataType>
<label translate="true">Require Verification</label>
</settings>
</column>
</columns>
<listingToolbar name="listing_top">
<massaction name="listing_massaction">
<action name="verify">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="type" xsi:type="string">verify</item>
<item name="label" xsi:type="string" translate="true">Verify</item>
<item name="url" xsi:type="url" path="verification/order/verify"/>
<item name="param" xsi:type="string">status</item>
<item name="confirm" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Status Verfication</item>
<item name="message" xsi:type="string" translate="true">
Are you sure you wan't to change the status of
selected items?
</item>
</item>
</item>
</argument>
</action>
</massaction>
</listingToolbar>
</listing>
etc/di.xml
<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="require_verification" xsi:type="string">sales_order.require_verification</item>
</argument>
</arguments>
</virtualType>
</config>
add column in existing table
etc/db_schema.xml
<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 Flat
Order">
<column xsi:type="smallint" name="require_verification" padding="5"
unsigned="true" nullable="true" identity="false" comment="Require Verification"/>
</table>
<table name="sales_order_grid" resource="sales" engine="innodb" comment="Sales Flat
Order Grid">
<column xsi:type="smallint" name="require_verification" padding="5"
unsigned="true" nullable="true" identity="false" comment="Require Verification"/>
</table>
</schema>
etc/events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
<observer name="require_verification_frontend"
instance="Moazzam\RequireVerification\Observer\Frontend\Verification"/>
</event>
</config>
Observer
<?php
declare(strict_types=1);
namespace Moazzam\RequireVerification\Observer\Frontend;
use Magento\Framework\App\State;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
/**
* PLEASE ENTER ONE LINE SHORT DESCRIPTION OF CLASS
*
* Class Verification
*/
class Verification implements ObserverInterface
{
/**
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @var State
*/
private State $state;
/**
* @param LoggerInterface $logger
* @param State $state
*/
public function __construct(
LoggerInterface $logger,
State $state
) {
$this->logger = $logger;
$this->state = $state;
}
/**
* Set the verification of custom attribute of sales_order
*
* @param Observer $observer
* @return void
* @throws LocalizedException
*/
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$order->setRequireVerification(1)->save();
$this->logger->info('order saving...' . PHP_EOL, [$this->state->getAreaCode(),
$observer->getEvent()->getOrder()->getData()]);
}
}
Controller
<?php
declare(strict_types=1);
namespace Moazzam\RequireVerification\Controller\Adminhtml\Order;
use Magento\Backend\App\Action;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory;
use Magento\Ui\Component\MassAction\Filter;
/**
* PLEASE ENTER ONE LINE SHORT DESCRIPTION OF CLASS
*
* Class Verify
*/
class Verify extends Action
{
/**
* @var CollectionFactory
*/
private CollectionFactory $collectionFactory;
/**
* @var Filter
*/
private Filter $filter;
/**
* @param Action\Context $context
* @param Filter $filter
* @param CollectionFactory $collection
*/
public function __construct(
Action\Context $context,
Filter $filter,
CollectionFactory $collection
) {
parent::__construct($context);
$this->collectionFactory = $collection;
$this->filter=$filter;
}
/**
* @return Redirect
* @throws LocalizedException
*/
public function execute(): Redirect
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
foreach ($collection as $order) {
if ($order->getStatus() == 'pending') {
$order->setRequireVerification(0)->save();
}
}
$this->messageManager->addSuccessMessage(__(
'A total of %1 record(s) have been modified.',
$collection->getSize()
));
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('sales/order/index');
return $resultRedirect;
}
}