Hello Magento Friends,
Today’s discussion is about How to Download Export File Directly Without a Cron Schedule in Magento 2.
Using Magento export functionality users can download CSV files of data. Export data into CSV file Programmatically in Magento 2.
At that time cron is scheduled to carry out the export operation. But if the user wants to download the CSV file without cron scheduling, you can use the following code.
Steps to Download Export File Directly Without Cron Schedule in Magento 2:
Step 1: First, we need to create a “di.xml” file inside our extension at the following path
app\code\Vendor\Extension\etc\adminhtml\
Then add the code as follows
1 2 3 4 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\ImportExport\Controller\Adminhtml\Export\Export" type="Vendor\Extension\Controller\Adminhtml\Export\Export" /> </config> |
Step 2: After that, we need to create an “ Export.php” file inside the extension Controller directory
app\code\Vendor\Extension\Controller\Adminhtml\Export\
And add the below code snippet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
<?php declare(strict_types=1); namespace Vendor\Extension\Controller\Adminhtml\Export; use Magento\Backend\App\Action\Context; use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; use Magento\Framework\App\Response\Http\FileFactory; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\MessageQueue\PublisherInterface; use Magento\ImportExport\Controller\Adminhtml\Export as ExportController; use Magento\ImportExport\Model\Export as ExportModel; use Magento\ImportExport\Model\Export\Entity\ExportInfoFactory; /** * Controller for export operation. */ class Export extends ExportController implements HttpPostActionInterface { /** * @var \Magento\Framework\App\Response\Http\FileFactory */ protected $fileFactory; /** * @var \Magento\Framework\Session\SessionManagerInterface */ private $sessionManager; /** * @var PublisherInterface */ private $messagePublisher; /** * @var ExportInfoFactory */ private $exportInfoFactory; /** * @param Context $context * @param FileFactory $fileFactory * @param \Magento\Framework\Session\SessionManagerInterface|null $sessionManager * @param PublisherInterface|null $publisher * @param ExportInfoFactory|null $exportInfoFactory */ public function __construct( Context $context, FileFactory $fileFactory, \Magento\Framework\Session\SessionManagerInterface $sessionManager = null, PublisherInterface $publisher = null, ExportInfoFactory $exportInfoFactory = null, ExportModel $exportModel) { $this->fileFactory = $fileFactory; $this->sessionManager = $sessionManager ?: \Magento\Framework\App\ ObjectManager::getInstance() ->get(\Magento\Framework\Session\SessionManagerInterface::class); $this->messagePublisher = $publisher ?: \Magento\Framework\App\ ObjectManager::getInstance() ->get(PublisherInterface::class); $this->exportInfoFactory = $exportInfoFactory ?: \Magento\Framework\App\ObjectManager::getInstance()->get( ExportInfoFactory::class); $this->exportModel = $exportModel; parent::__construct($context); } /** * Load data with filter applying and create file for download. * * @return \Magento\Backend\Model\View\Result\Redirect */ public function execute() { if ($this->getRequest()->getPost(ExportModel::FILTER_ELEMENT_GROUP)) { try { $params = $this->getRequest()->getParams(); //$model = $this->_objectManager->create(\Magento\ImportExport\Model\ Export::class); $this->exportModel->setData($this->getRequest()->getParams()); $this->sessionManager->writeClose(); return $this->fileFactory->create( $this->exportModel->getFileName(), $this->exportModel->export(), \Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR, $this->exportModel->getContentType()); /** @var ExportInfoFactory $dataObject */ $dataObject = $this->exportInfoFactory->create( $params['file_format'], $params['entity'], $params['export_filter']); $this->messagePublisher->publish('import_export.export', $dataObject); $this->messageManager->addSuccessMessage( __('Message is added to queue, wait to get your file soon')); } catch (\Exception $e) { $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e); $this->messageManager->addError(__('Please correct the data sent value.')); } } else { $this->messageManager->addError(__('Please correct the data sent value.')); } /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $resultRedirect->setPath('adminhtml/*/index'); return $resultRedirect; } } |
Conclusion:
By doing so, you can Download Export File Directly Without Cron Schedule in Magento 2. If you have any doubt regarding the above steps, contact me via the comment section. I will be right back to you. Share the article with your friends and stay in touch with us!
Happy Coding!