1

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?

Thanks!

7
  • 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

1 Answer 1

3

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:

app/code/Pawan/GenerateCSV/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Pawan_GenerateCSV',
__DIR__
);

app/code/Pawan/GenerateCSV/etc/module.xml

<?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">
   </module>
</config>

app/code/Pawan/GenerateCSV/etc/events.xml

<?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'>
        <observer
                name='sales_order_place_after'
                instance='Pawan\GenerateCSV\Observer\GenerateCSV'
        />
    </event>
</config>

app/code/Pawan/GenerateCSV/Observer/GenerateCSV.php

<?php

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($order_id);
    $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!

4
  • 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

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.