1

how to in API create_at time change. because when I am set local India/Kolkata then place an order after that created_at properly show sales order admin grid and view page. but in postment api is not showing the proper date. so please check the below screenshot.

enter image description here

enter image description here

1 Answer 1

2

The Magento API always returns the dates in UTC (like it stored in database)

You can convert the date to local time on client side or try to create plugin and provide converted data in Extension Attributes

app/code/Acme/StackExchange/etc/di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="Acme_StackExchange_Plugin_Sales_Api_OrderRepositoryPlugin"
                type="Acme\StackExchange\Plugin\Sales\Api\OrderRepositoryPlugin" />
    </type>
</config>

app/code/Acme/StackExchange/etc/extension_attributes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="created_at_store" type="string" />
    </extension_attributes>
</config>

app/code/Acme/StackExchange/Plugin/Sales/Api/OrderRepositoryPlugin.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Plugin\Sales\Api;

use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

class OrderRepositoryPlugin
{
    protected const DATETIME_FORMAT = 'Y-m-d H:i:s';

    protected TimezoneInterface     $timezone;
    protected string                $locale;
    protected OrderExtensionFactory $orderExtensionFactory;

    public function __construct(
        TimezoneInterface $timezone,
        ResolverInterface $localeResolver,
        OrderExtensionFactory $orderExtensionFactory
    ) {
        $this->timezone = $timezone;
        $this->locale   = $localeResolver->getLocale();
        $this->orderExtensionFactory = $orderExtensionFactory;
    }

    public function afterGet(OrderRepositoryInterface $subject, OrderInterface $result): OrderInterface
    {
        $extensionAttributes = $result->getExtensionAttributes() ?: $this->orderExtensionFactory->create();

        $createdAtStore = $this->timezone->date(
            new \DateTime($result->getCreatedAt(), new \DateTimeZone('UTC')),
            $this->locale
        );

        $extensionAttributes->setCreatedAtStore($createdAtStore->format(self::DATETIME_FORMAT));

        $result->setExtensionAttributes($extensionAttributes);

        return $result;
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.