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!

  • Do you want to sync order data to another system? – Khoa TruongDinh Dec 12 '19 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 – Gerardo Siano Dec 12 '19 at 4:07
  • you can use sales_order_place_after observer and get required data in CSV – Pawan Dec 12 '19 at 4:14
  • Uhm, can you please tell me more about it? – Gerardo Siano Dec 12 '19 at 4:18
  • please see my answer, I just get order data. you can put CSV logic based on your requirement. – Pawan Dec 12 '19 at 5:07
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!

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.