2

I wanted to save the orders from magento2 to my local Database.

I am planning to Create a module and add Observer to catch once a order is placed.

I don't know much about the Events & observer, just gone through this link

Please help me is there any Event already given by Magento2 after new Order. if there is no default event for Order placement Where do I have to write a event for new order.

Thanks in Advance.

Edit:

app\code\Autosynch\Sale\registration.php

<?php /**
* Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details.
*/
\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Autosynch_sale',
__DIR__
);

app\cpde\Autosynch\Sale\Observer\OrderPlaceAfter.php

<?php

namespace Autosynch\Sale\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use \Psr\Log\LoggerInterface;


class OrderPlaceAfter implements ObserverInterface
{
    public function execute(Observer $observer, LoggerInterface $logger)
    {
        $order = $observer->getEvent()->getOrder();

        $logger->info($order->getData());
        $logger->debug($order->getData());

        var_dump($order->getData());
        exit;
    }
}

app\code\Autosynch\Sale\etc\module.xml

<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Autosynch_Sale" setup_version="0.0.1">
    </module>
</config>

app\code\Autosynch\Sale\etc\event.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="checkout_submit_all_after">
        <observer name="modulename_sales_order_place_after" instance="Autosynch\Sale\Observer\OrderPlaceAfter" />
    </event>
</config>

Please help me where I am missing.

2

Create a file Autosynch/Sale/etc/event.xml and put below code,

<?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_submit_all_after">
        <observer name="autosynch_sales_order_place_after" instance="Autosynch\Sale\Observer\OrderPlaceAfter" />
    </event>
</config>

And then create Observer class OrderPlaceAfter.php in Autosynch/Sale/Observer.

    <?php
    namespace Autosynch\Sale\Observer;

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\Event\Observer;

    class OrderPlaceAfter implements ObserverInterface
    {
        protected $_logger;

        /**
         * @param \Psr\Log\LoggerInterface $_logger
         */

        public function __construct(
            \Psr\Log\LoggerInterface $_logger
        ){
            $this->_logger = $_logger;
        }


        public function execute(Observer $observer)
        {
            $order = $observer->getEvent()->getOrder();

            $this->_logger->info($order->getData());
            $this->_logger->debug($order->getData());

            //var_dump($order->getData());
            //exit;
        }
    }

Hope this help You.

  • Thank you very much, this will help to get started. I was wondering how I need to test and see how Data is coming, So need to parse it and Send to another service and Save Data in Oracle Database. – JB Pakalapati Jun 7 '18 at 9:46
  • every time the order placed in magento, the observer will be called. You can analyze the order data by doing var_dump($order->getData()); exit; it will give array of order information. – Prasanta Hatui Jun 7 '18 at 9:52
  • Is there any way to debug to see the array data or to print some where. what this line will do var_dump($order->getData());. bu the way i need to do all my operations before exit; right? – JB Pakalapati Jun 7 '18 at 10:01
  • You can create log . this thienphucvx.com/magento-2-logging-to-a-custom-file will help you. – Prasanta Hatui Jun 7 '18 at 10:14
  • I am getting this error Fatal error: Declaration of Autosynch\Sale\Observer\OrderPlaceAfter::execute() must be compatible with Magento\Framework\Event\ObserverInterface::execute(Magento\Framework\Event\Observer $observer) in C:\xampp\htdocs\Magento-CE\app\code\Autosynch\Sale\Observer\OrderPlaceAfter.php on line 11 Let me where I ma going wrong – JB Pakalapati Jun 10 '18 at 6:27
2

I assume you know how to create observer. You can catch event sales_order_place_after for your task.

Reference

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.