1

I am using magento 2.4.4. I am getting wrong "purchase date in CSV".

Purchase date on order - Aug 31, 2021 9:19:46 PM. On csv - 2021-09-01 01:19:46.

From where its data gets set in csv ? enter image description here

8

3 Answers 3

1
+50

I have fixed using this, try.

File name :Magento\Ui\Model\Export\MetadataProvider

public function getRowData(DocumentInterface $document, $fields, $options): array
        {
            $row = [];
            foreach ($fields as $column) {
                if (isset($options[$column])) {
                    $key = $document->getCustomAttribute($column)->getValue();
                    if (isset($options[$column][$key])) {
                        $row[] = $options[$column][$key];
                    } else {
                        $row[] = $key;
                    }
                } else {
                    if($column == "created_at"){
                        $convertedDate = $this->localeDate->date(
                        new DateTime($document->getCustomAttribute($column)->getValue(), new DateTimeZone('GMT')),
                        $this->locale,
                        true
                        );
                        $created_at = new DateTime($convertedDate->format($this->dateFormat));
                        $row[] = $created_at->format('Y-m-d H:i:s');
                    }else{
                        $row[] = $document->getCustomAttribute($column)->getValue();
                    }
                }
            }
    
            return $row;
        }
1

Magento will export the created date in UTC. if your store use another timezone, the time will be diffrent in the order grid (timezone of your store) and in the exported csv. To get the order created date in your timezone, you can add a custom column to the csv. See here or here.

1

Just checked in version 2.4.3 and 2.4.4 and both versions works correct for Order and export into CSV

First: check your locale in configuration

Second: check the date in database (sales_order)

How and why it should work:

See vendor/magento/module-sales/etc/adminhtml/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\Ui\Model\Export\MetadataProvider">
        <arguments>
            <argument name="data" xsi:type="array">
                <item name="sales_order_grid" xsi:type="array">
                    <item name="created_at" xsi:type="string">created_at</item>
                </item>
                <!-- ... -->
            </argument>
        </arguments>
    </type>
</config>

Please check if you didn't overwrite this in di or has incorrect plugin for

See implementation in \Magento\Ui\Model\Export\MetadataProvider

namespace Magento\Ui\Model\Export;

// ...

class MetadataProvider
{
    // ...
    public function convertDate($document, $componentName): void
    {
        if (!isset($this->data[$componentName])) {
            return;
        }
        foreach ($this->data[$componentName] as $field) {
            $fieldValue = $document->getData($field);
            if (!$fieldValue) {
                continue;
            }
            $convertedDate = $this->localeDate->date(
                new DateTime($fieldValue, new DateTimeZone('UTC')),
                $this->locale,
                true
            );
            $document->setData($field, $convertedDate->format($this->dateFormat));
        }
    }
}

The method calls in \Magento\Ui\Model\Export\ConvertToCsv::getCsvFile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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