I need to create a CSV file on every order I receive. How can I do this? Can you guys please suggest me a way to do this or an existing module that does that?


  • Do you want to sync order data to another system? Dec 12, 2019 at 3:32
  • No. I just need to automatically create a CSV from the last order. This file needs to be uploaded to the server Dec 12, 2019 at 4:07
  • you can use sales_order_place_after observer and get required data in CSV
    – Pawan
    Dec 12, 2019 at 4:14
  • Uhm, can you please tell me more about it? Dec 12, 2019 at 4:18
  • please see my answer, I just get order data. you can put CSV logic based on your requirement.
    – Pawan
    Dec 12, 2019 at 5:07

2 Answers 2


You need to create a small module, which will use event-observer feature of magento. we will put an observer which will get trigger when any order will get placed!

create following files:




<?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="Pawan_GenerateCSV" setup_version="1.0.0">


<?xml version='1.0'?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='urn:magento:framework/Event/etc/events.xsd'>
   <event name='sales_order_place_after'>



namespace Pawan\GenerateCSV\Observer;

use Magento\Framework\Event\ObserverInterface;

class GenerateCSV implements ObserverInterface
 protected $_objectManager;
 private $logger;
 private $productFactory;

  public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Catalog\Model\ProductFactory $productFactory
  ) {
      $this->_objectManager = $objectManager;
      $this->logger = $logger;
      $this->productFactory = $productFactory;

  public function execute(\Magento\Framework\Event\Observer $observer)
    $order = $observer->getEvent()->getOrder();
    $order_id = $order->getIncrementId();
    $this->logger->info("observer working");  


Explanation: Now you have all Order data in observer class, form here you can get order, product information and can get data in CSV

Hope above will help!

  • Hi Pawan, How can I get the item id, quantity and price from this? May 24, 2021 at 15:43
  • You can get all items by $orderItems = $order->getAllItems();. in foreach loop , you can get item price, id etc..
    – Pawan
    May 25, 2021 at 6:27
  • I am already trying with foreach ($order->getAllItems() as $item) { $productIds[] = $item->getProductId(); $product_qty[] = $item->getQtyOrdered(); $product_price[] = $item->getPrice(); } but not able to get the price. May 25, 2021 at 10:19
  • If you are not getting price, You may need to load Order OR inject Magento\Sales\Api\OrderRepositoryInterface $orderRepository in your observer and get Order like $order = $this->orderRepository->get($orderId);
    – Pawan
    May 25, 2021 at 10:52

You can follow this below link -


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.