0

Here am trying to override a vendor file that is

vendor/magento/module-ui/Model/Export/ConvertToCsv.php

etc/di.xml file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Ui\Model\Export\ConvertToCsv" type="Pravendor\PRA\Model\Export\ConvertToCsv" />
</config>

overridden file path

app/code/Pravendor/PRA/Model/Export/ConvertToCsv.php

<?php
namespace Pravendor\PRA\Model\Export;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Ui\Model\Export\MetadataProvider;
use Magento\Ui\Model\Export\ConvertToCsv as ConvertToCsvParent;
/**
 * Class ConvertToCsv
 */
class ConvertToCsv extends ConvertToCsvParent
{
    /**
     * @var DirectoryList
     */
    protected $directory;
    /**
     * @var MetadataProvider
     */
    protected $metadataProvider;
    /**
     * @var int|null
     */
    protected $pageSize = null;
    /**
     * @var Filter
     */
    protected $filter;
    /**
     * @var Product
     */
    private $productHelper;
    /**
     * @var TimezoneInterface
     */
    private $timezone;
    /**
     * @param Filesystem $filesystem
     * @param Filter $filter
     * @param MetadataProvider $metadataProvider
     * @param int $pageSize
     * @throws FileSystemException
     */
    public function __construct(
        Filesystem $filesystem,
        Filter $filter,
        MetadataProvider $metadataProvider,
        Product $productHelper,
        TimezoneInterface $timezone,
        $pageSize = 200
    ) {
        $this->filter = $filter;
        $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
        $this->metadataProvider = $metadataProvider;
        $this->pageSize = $pageSize;
        $this->productHelper = $productHelper;
        parent::__construct($filesystem, $filter, $metadataProvider, $pageSize);
        $this->timezone = $timezone;
    }
    public function getCsvFile()
    {

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/PRAcsv.log'); 
        $logger = new \Zend\Log\Logger(); $logger->addWriter($writer); 
        $logger->info('Your text message'); 


        $component = $this->filter->getComponent();


        $name = md5(microtime());
        $file = 'export/'. $component->getName() . $name . '.csv';

        $this->filter->prepareComponent($component);
        $this->filter->applySelectionOnTargetProvider();
        $dataProvider = $component->getContext()->getDataProvider();
        //exit(get_class($dataProvider));
        $fields = $this->metadataProvider->getFields($component);

        $options = $this->metadataProvider->getOptions();

        $this->directory->create('export');
        $stream = $this->directory->openFile($file, 'w+');
        $stream->lock();
        $stream->writeCsv($this->metadataProvider->getHeaders($component));
        $i = 1;
        $searchCriteria = $dataProvider->getSearchCriteria()
            ->setCurrentPage($i)
            ->setPageSize($this->pageSize);
        $totalCount = (int) $dataProvider->getSearchResult()->getTotalCount();
        while ($totalCount > 0) {
            $items = $dataProvider->getSearchResult()->getItems();
            //  echo '<pre>'; print_r(get_class($dataProvider)); exit;
            foreach ($items as $item) {
                if($component->getName()=='sales_order_grid') {
                    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                    $order = $objectManager->create('Magento\Sales\Model\Order')->load($item->getEntityId());
                    $items123 = $order->getAllItems();
                    $skuArray = [];

                    foreach ($items123 as $key => $item1) {
                        $skuArray[] = $item1->getSku();

                    }
                    $export_status = implode(", ", $skuArray);
                    $item->setSku($export_status);
                }
                $this->metadataProvider->convertDate($item, $component->getName());
                $stream->writeCsv($this->metadataProvider->getRowData($item, $fields, $options));
            }
            $searchCriteria->setCurrentPage(++$i);
            $totalCount = $totalCount - $this->pageSize;
        }
        $stream->unlock();
        $stream->close();

        return [
            'type' => 'filename',
            'value' => $file,
            'rm' => true  // can delete file after use
        ];
    }

}

but file is not overridden, where am doing wrong. can I get help? Thank you in advance.

1 Answer 1

1

Please add di.xml file in adminhtml instead of global.

etc/adminhtml/di.xml

and run below command

php bin/magento setup:di:compile

php bin/magento cache:clean

2
  • Thank you so much @Gohil Rajesh, its successful override. Nov 3, 2021 at 5:58
  • great !! happy to hear Nov 3, 2021 at 5:59

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.