1

I have created a new module.

app\code\Sale\Paymentext\etc\webapi_rest\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">
<type name="Magento\Sales\Model\OrderRepository">
    <plugin name="Sale_Paymentexte_Plugin_Webapi_Magento_Sales_Model_OrderRepository" type="Sale\Paymentext\Plugin\Webapi\Magento\Sales\Model\OrderRepository" sortOrder="10" disabled="false"/>
</type>
</config>

app\code\Sale\Paymentext\Plugin\Webapi\Magento\Sales\Model\OrderRepository

<?php

declare(strict_types=1);

namespace Sale\Paymentext\Plugin\Webapi\Magento\Sales\Model;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\OrderRepository as ModelOrderRepository;
use Magento\Sales\Api\Data\OrderSearchResultInterfaceFactory as SearchResultFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Tax\Api\OrderTaxManagementInterface;
use Magento\Payment\Api\Data\PaymentAdditionalInfoInterface;
use Magento\Payment\Api\Data\PaymentAdditionalInfoInterfaceFactory;
use Magento\Sales\Model\Order\ShippingAssignmentBuilder;

class OrderRepository
{   

protected $order;

protected $locale;

protected $template;

protected $searchResultFactory = null; 

private $extensionAttributesJoinProcessor;

private $collectionProcessor;

private $orderExtensionFactory;

private $orderTaxManagement;

private $paymentAdditionalInfoFactory;

private $shippingAssignmentBuilder;

/**
 * Constructor
 *
 * @param Metadata $metadata
 * @param \Magento\Framework\View\Element\Template $template,
 * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $locale,
 * @param SearchResultFactory $searchResultFactory,
 * @param \Magento\Sales\Api\Data\OrderExtensionFactory|null $orderExtensionFactory
 * @param CollectionProcessorInterface|null $collectionProcessor
 * @param OrderTaxManagementInterface|null $orderTaxManagement
 * @param PaymentAdditionalInfoInterfaceFactory|null $paymentAdditionalInfoFactory
 * @param JoinProcessorInterface $extensionAttributesJoinProcessor
 */
public function __construct(
    \Magento\Framework\View\Element\Template $template,
    \Magento\Framework\Stdlib\DateTime\TimezoneInterface $locale,
    \Magento\Sales\Api\Data\OrderInterface $order,
    SearchResultFactory $searchResultFactory,
    JoinProcessorInterface $extensionAttributesJoinProcessor = null,
    \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory = null,
    OrderTaxManagementInterface $orderTaxManagement = null,
    PaymentAdditionalInfoInterfaceFactory $paymentAdditionalInfoFactory = null,
    CollectionProcessorInterface $collectionProcessor = null
) { 
    $this->order = $order;
    $this->searchResultFactory = $searchResultFactory;
    $this->locale = $locale;
    $this->template = $template;
    $this->orderExtensionFactory = $orderExtensionFactory ?: ObjectManager::getInstance()
        ->get(\Magento\Sales\Api\Data\OrderExtensionFactory::class);
    $this->orderTaxManagement = $orderTaxManagement ?: ObjectManager::getInstance()
            ->get(OrderTaxManagementInterface::class); 
    $this->paymentAdditionalInfoFactory = $paymentAdditionalInfoFactory ?: ObjectManager::getInstance()
            ->get(PaymentAdditionalInfoInterfaceFactory::class);               
    $this->collectionProcessor = $collectionProcessor ?: ObjectManager::getInstance()
            ->get(\Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class);
    $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor
            ?: ObjectManager::getInstance()->get(JoinProcessorInterface::class);
}

/**
 * Load entity
 *
 * @param int $id
 * @return \Magento\Sales\Api\Data\OrderInterface
 * @throws \Magento\Framework\Exception\InputException
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function afterGet(
    ModelOrderRepository $orderRepository,
    OrderInterface $order
) { 
    $order = $this->order->load($order->getId());
    $date = $this->locale->date($order->getCreatedAt());         
    $createdAt = date_format($date,'Y-m-d H:i:s');
    $order->setCreatedAt($createdAt);

    $date = $this->locale->date($order->getUpdatedAt());
    $updatedAt = date_format($date,'Y-m-d H:i:s');
    $order->setUpdatedAt($updatedAt);

    $this->setOrderTaxDetails($order);
    $this->setShippingAssignments($order);
    $this->setPaymentAdditionalInfo($order);

    return $order;
}

public function afterGetList(
    \Magento\Sales\Model\OrderRepository $subject,
    $result,
    $searchCriteria
) {
    $searchResult = $this->searchResultFactory->create();
    $this->extensionAttributesJoinProcessor->process($searchResult);
    $this->collectionProcessor->process($searchCriteria, $searchResult);
    $searchResult->setSearchCriteria($searchCriteria);  
        
    foreach ($searchResult as $order) {

        $date = $this->locale->date($order->getCreatedAt());
        $createdAt = date_format($date,'Y-m-d H:i:s');
        $order->setCreatedAt($createdAt);

        $date = $this->locale->date($order->getUpdatedAt());
        $updatedAt = date_format($date,'Y-m-d H:i:s');
        $order->setUpdatedAt($updatedAt);

        $this->setShippingAssignments($order);
        $this->setOrderTaxDetails($order);
        $this->setPaymentAdditionalInfo($order);

    }
    
    return $searchResult;
}

private function setOrderTaxDetails(OrderInterface $order)
{
    $extensionAttributes = $order->getExtensionAttributes();
    if ($extensionAttributes === null) {
        $extensionAttributes = $this->orderExtensionFactory->create();
    }
    $orderTaxDetails = $this->orderTaxManagement->getOrderTaxDetails($order->getEntityId());
    $appliedTaxes = $orderTaxDetails->getAppliedTaxes();

    $extensionAttributes->setAppliedTaxes($appliedTaxes);
    if (!empty($appliedTaxes)) {
        $extensionAttributes->setConvertingFromQuote(true);
    }

    $items = $orderTaxDetails->getItems();
    $extensionAttributes->setItemAppliedTaxes($items);

    $order->setExtensionAttributes($extensionAttributes);
}

private function setShippingAssignments(OrderInterface $order)
{

    /** @var OrderExtensionInterface $extensionAttributes */
    $extensionAttributes = $order->getExtensionAttributes();

    if ($extensionAttributes === null) {
        $extensionAttributes = $this->orderExtensionFactory->create();
    } elseif ($extensionAttributes->getShippingAssignments() !== null) {
        return;
    }
    /** @var ShippingAssignmentInterface $shippingAssignment */
    $shippingAssignments = $this->getShippingAssignmentBuilderDependency();

    $shippingAssignments->setOrderId($order->getEntityId());
    $extensionAttributes->setShippingAssignments($shippingAssignments->create());
    $order->setExtensionAttributes($extensionAttributes);
}

private function setPaymentAdditionalInfo(OrderInterface $order): void
{

    $extensionAttributes = $order->getExtensionAttributes();
    if ($extensionAttributes === null) {
        $extensionAttributes = $this->orderExtensionFactory->create();
    }

    $paymentAdditionalInformation = [];
    $payment = $order->getPayment();

    if ($payment) {
        $paymentAdditionalInformation = $payment->getAdditionalInformation();
    }

    $objects = [];
    foreach ($paymentAdditionalInformation as $key => $value) {
        /** @var PaymentAdditionalInfoInterface $additionalInformationObject */
        $additionalInformationObject = $this->paymentAdditionalInfoFactory->create();
        $additionalInformationObject->setKey($key);

        if (!is_string($value)) {
            $value = $this->serializer->serialize($value);
        }
        $additionalInformationObject->setValue($value);

        $objects[] = $additionalInformationObject;
    }
    $extensionAttributes->setPaymentAdditionalInfo($objects);
    $order->setExtensionAttributes($extensionAttributes);
}

private function getShippingAssignmentBuilderDependency()
{
    if (!$this->shippingAssignmentBuilder instanceof ShippingAssignmentBuilder) {
        $this->shippingAssignmentBuilder = \Magento\Framework\App\ObjectManager::getInstance()->get(
            \Magento\Sales\Model\Order\ShippingAssignmentBuilder::class
        );
    }
    return $this->shippingAssignmentBuilder;
}

}

Above code of work create_at so please check below screenshot.

before image

enter image description here

after image

enter image description here

sales admin grid

enter image description here

now i want to change date formatted in extension_attributes => items => created_at so please help me.

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.