This functionality will be done by and event observer. And Event is “checkout_onepage_controller_success_action”.
In this event you can get the current order data and you will be able to export this data in to csv.
Step 1: For add the event you have to make a events.xml in your module and paste the below code into it.
1 2 3 4 5 6 |
<?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="checkout_onepage_controller_success_action"> <observer name="customorderexport" instance="Magemonkeys\Customexport\Observer\Orderexport" /> </event> </config> |
Step 2: Now you have to create a Orderexport.php file in Observer folder and place the below code into it.
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 |
<?php namespace Magemonkeys\Customexport\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem; use Magento\Framework\Filesystem\Directory\WriteInterface; use Magento\Store\Model\ScopeInterface; class Orderexport implements ObserverInterface { protected $_request; protected $_order; protected $_productRepository; protected $_scopeConfig; protected $_customer; protected $_storemanager; public function __construct( \Magento\Framework\App\RequestInterface $request, \Magento\Sales\Model\Order $order, \Magento\Framework\App\Response\Http\FileFactory $fileFactory, Filesystem $filesystem, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Customer\Model\CustomerFactory $customer, \Magento\Store\Model\StoreManagerInterface $storemanager, \Magento\Catalog\Model\ProductRepository $productRepository ) { $this->_scopeConfig = $scopeConfig; $this->_customer = $customer; $this->_storemanager = $storemanager; $this->_request = $request; $this->_order = $order; $this->_fileFactory = $fileFactory; $this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); $this->_productRepository = $productRepository; } public function execute(\Magento\Framework\Event\Observer $observer) { $post = $this->_request->getPost(); $orderids = $observer->getEvent()->getOrderIds(); foreach($orderids as $orderid){ $order = $this->_order->load($orderId); $websiteID = $this->_storemanager->getStore()->getWebsiteId(); $customer = $this->_customer->create()->setWebsiteId($websiteID)->loadByEmail($order->getCustomerEmail()); $customerId = $customer->getId(); // using this $customerPoNumber = $customer->getCustomerNumber(); // using this $headers = array('Magento_Account_Number','Back_Office_Account_Number','Transaction_Date','Transaction_Time','Magento_Transaction_Number','Client_Reference_Number','Item_Number','Quantity_Sold','Selling_Price','Discount'); $name = strtotime("now"); $file = 'customorderexport/'.$name.'_detailed_orderexport.csv'; $this->directory->create('customorderexport'); $stream = $this->directory->openFile($file, 'w+'); $stream->lock(); $stream->writeCsv($headers); $items = $order->getAllItems(); foreach ($items as $item) { $orderdetail['Magento_Account_Number'] = $orderdata->getData('created_at'); $orderdetail['Order_Number'] = $orderdata->getData('increment_id'); $orderdetail['Transaction_Date'] = date('Y-m-d',strtotime($order->getCreatedAt())); $orderdetail['Transaction_Time'] = date('h:i A',strtotime($order->getCreatedAt())); $orderdetail['Magento_Transaction_Number'] = $order->getIncrementId(); $orderdetail['Client_Reference_Number'] = $customerPoNumber; $id = $item->getProdcutId(); $product = $this->_productRepository->getById($id); $orderdetail['Item_Number'] = $product->getAutoKoolNumber(); $orderdetail['Quantity_Sold'] = $item->getQtyOrdered(); $orderdetail['Selling_Price'] = $item->getPrice(); $orderdetail['Discount'] = $order->getDiscountAmount(); $stream->writeCsv($orderdetail); } $stream->unlock(); $stream->close(); } } } ?> |
That’s it.
Note- I have use the field and data as per my requirements. You will change this as per your requirements.
[crayon-63e0931182ac2821395151/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.