0

I am trying to get order details in observer (event: checkout_submit_all_after). I am trying to print the order details in debug.log, but nothing printed.

Please refer this know about my complete module structure. I could change observer class functionality like below for getting order details.

app/code/Gworks/Sms/Observer/LogAddMessage.php

<?php
namespace Gworks\Sms\Observer;
use Magento\Framework\Event\ObserverInterface;
class LogAddMessage implements ObserverInterface
{
    protected $_logger;
    public function __construct(
    \Psr\Log\LoggerInterface $logger, //log injection
    array $data = []
    ) {
    $this->_logger = $logger;
       // parent::__construct($data);
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $this->_logger->addDebug('getplace oreder event occured successfully!');
    }

    public function getOrder(\Magento\Framework\Event\Observer $observer)
    {
    $order = $observer->getEvent()->getOrder();
    $order_id = $order->getIncrementId();
    $this->logger->addDebug($order_id);       
    }
}

debug.log

[2016-04-26 10:14:10] main.DEBUG: getplace oreder event occured successfully! {"is_exception":false} [] [2016-04-26 10:14:10] main.DEBUG: cache_invalidate: {"method":"POST","url":"http://magentodev.gworks.mobi/magento2/rest/default/V1/carts/mine/payment-information","invalidateInfo":{"tags":["catalog_category_2","catalog_category_26","catalog_category_30","catalog_category_34","catalog_category"],"mode":"matchingAnyTag"},"is_exception":false} []

when define order details in execute method it will throw the below error while place order

enter image description here

[2016-04-26 11:37:42] main.CRITICAL: exception 'Exception' with message 'Report ID: webapi-571f53065307a; Message: Notice: Array to string conversion in /var/www/html/magento2/vendor/monolog/monolog/src/Monolog/Logger.php on line 277' in /var/www/html/magento2/vendor/magento/framework/Webapi/ErrorProcessor.php:194
Stack trace:
#0 /var/www/html/magento2/vendor/magento/framework/Webapi/ErrorProcessor.php(139): Magento\Framework\Webapi\ErrorProcessor->_critical(Object(Exception))
#1 /var/www/html/magento2/vendor/magento/module-webapi/Controller/Rest.php(163): Magento\Framework\Webapi\ErrorProcessor->maskException(Object(Exception))
#2 /var/www/html/magento2/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#3 /var/www/html/magento2/vendor/magento/framework/App/Http.php(115): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#4 /var/www/html/magento2/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#5 /var/www/html/magento2/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#6 {main} [] []

complete module in Github

Thanks

1

Use $order = $observer->getEvent()->getOrder();

instead of $order = $observer->getOrder();

Also,you can use checkout_submit_all_after event

  • still the problem remains, I have updated my question. @Amit Bera – Bilal Usean Apr 26 '16 at 10:04
  • Can u try with checkout_submit_all_after – Amit Bera Apr 26 '16 at 10:16
  • yes event: checkout_submit_all_after – Bilal Usean Apr 26 '16 at 10:18
  • let me check?.... – Amit Bera Apr 26 '16 at 10:24
  • Can u please share ur source code at github' – Amit Bera Apr 26 '16 at 10:25
1

as per my understanding instead of using getOrder() (method), Use only one method execute()..

you can make as many as functions (methods) you want, but just use in the same class.. hope this will solve the problem

  • I had already tried, but when click place order it throws error, success page did not come when I do like this – Bilal Usean Apr 26 '16 at 10:56
1

Use the sales_order_save_after instead of checkout_submit_all_after observer.

and create a events.xml file inside etc folder and write 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="sales_order_save_after">
        <observer name="save_order" instance="VendorName\ModuleName\Model\Observer\AfterPlaceOrderObserver" />
    </event>
</config>

create a AfterPlaceOrderObserver.php file inside Model/Observer/ folder and write the below code

    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        /* @var $order \Magento\Sales\Model\Order */
            $order = $observer->getEvent()->getOrder();
            $order_data = $order->getData();

file_put_contents('order.txt', print_r("\n" . $order_data,true), FILE_APPEND);
    }
  • thanks, I have tried but it throw error, I have updated the error details in my question – Bilal Usean Apr 26 '16 at 11:45
  • @BilalUsean, check your exception.log file the ReportID: with error will be print. post that error. – Bojjaiah Apr 26 '16 at 11:51
  • @BilalUsean And please see my updated answer copy and paste it it will work. from my side it's working perfect.. – Bojjaiah Apr 26 '16 at 11:57
  • I have tried your solution but no luck still the same problem remain – Bilal Usean Apr 26 '16 at 12:15
  • @BilalUsean the error throwing clearly the data coming up with array of data but you are storing with string. So use file_put_contents('order.txt', print_r("\n" . $result,true), FILE_APPEND); this function it will create the file in your magento2 root folder. try it definitely work. please let me know if it's not working. see my answer updated. – Bojjaiah Apr 26 '16 at 12:38
0

Its working finaly

app/code/Gworks/Sms/Observer/LogAddMessage.php

<?php
    namespace Gworks\Sms\Observer;
use Magento\Framework\Event\ObserverInterface;
class LogAddMessage implements ObserverInterface
{
    protected $_logger;
    protected $order;
    public function __construct(\Magento\Sales\Model\Order $order,
    \Psr\Log\LoggerInterface $logger, //log injection
    array $data = []
    ) {
    $this->_logger = $logger;
       // parent::__construct($data);
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
/* @var $order \Magento\Sales\Model\Order */
        $order = $observer->getEvent()->getOrder();
        $order_id = $order->getIncrementId();
        $this->_logger->info($order_id);
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->get('Magento\Sales\Model\Order');
        $order_information = $order->loadByIncrementId($order_id);
        $this->_logger->info('Order Info', $order_information->getData());
    }

}

order details are printed in system.log

note: thanks to all for give supports :-)

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.