In this blog, we will give you the proper solution of Magento 2 Export Orders CSV programmatically.
You can programmatically export orders in CSV format from your Magento 2 store using the Magento 2 API.
Here are a few steps for Programmatically Export Orders into CSV in Magento 2.
Step 1 : Set up a custom module.Create module.xml file in app/code/Custom/Module/etc/. <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Custom_Module" setup_version="1.0.0"> </module> </config> Create registration.php file in app/code/Custom/Module/. \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Custom_Module', __DIR__ ); |
Step 2 : Create routes for your moduleCreate routes.xml file in app/code/Custom/Module/etc/frontend/. <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <router id="standard"> <route frontName="custom" id="custom"> <module name="Custom_Module"/> </route> </router> </config> |
Step 3 : Create a controller to Export order Data in CSV format.Create controller ExportOredrs.php in app/codeCustom/Module/Controller/Index/. <?php> namespace Custom\Module\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\Response\Http\FileFactory; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Framework\Api\SearchCriteriaBuilder; class ExportOrders extends Action { protected $fileFactory; protected $orderRepository; protected $searchCriteriaBuilder; protected $_filesystem; public function __construct( Context $context, FileFactory $fileFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\Filesystem $filesystem ) { $this->fileFactory = $fileFactory; $this->orderRepository = $orderRepository; $this->searchCriteriaBuilder = $searchCriteriaBuilder; $this->_filesystem = $filesystem; parent::__construct($context); } public function execute() { $searchCriteria = $this->searchCriteriaBuilder->create(); $orders = $this->orderRepository->getList($searchCriteria)->getItems(); $csvData = []; $csvData[] = ['Order ID','Customer Email','Order Total']; foreach ($orders as $order) { $csvData[] = [ $order->getEntityId(), $order->getCustomerEmail(), $order->getGrandTotal() ]; } $fileName = 'orders.csv'; $filePath = 'export/' . $fileName; $directory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); $stream = $directory->openFile($filePath, 'w+'); foreach ($csvData as $rowData) { $stream->writeCsv($rowData); } $stream->close(); $content = [ 'type' => 'filename', 'value' => $filePath, 'rm' => true ]; $response = $this->fileFactory->create($fileName, $content, DirectoryList::VAR_DIR); return $response; } } In this controller, you can change the below data according to your need. Ex:-If you want to add one more field like `Order Status` then you can Use below code. $csvData = []; $csvData[] = ['Order ID', 'Customer Email', 'Order Total', 'Order Status']; foreach ($orders as $order) { $csvData[] = [ $order->getEntityId(), $order->getCustomerEmail(), $order->getGrandTotal(), $order->getStatus() ]; } |
Now you can export your store order using URL - {your_base_url}/custom/index/exportorders
We hope you now have a clear understanding of how to export orders to CSV in Magento 2 programmatically.
You can easily perform order exports using the MageAnts Export Order Magento 2 Extension.