3

In Magento, I have to get order items in the observer.

I am trying two method sales_order_place_after and checkout_submit_all_after.

Both events is not working.

events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer name="cart_product" instance="Example\Details\Observer\CartProduct" />
    </event>
    <event name="checkout_submit_all_after">
        <observer name="oreder_item" instance="Example\Details\Observer\OrderItem" />
    </event>
</config>

OrderItem.php

<?php

namespace Example\Details\Observer;

use Magento\Framework\Event\ObserverInterface;

class OrderItem implements ObserverInterface
{
    public function __construct(
        \Magento\Framework\App\Request\Http $request,
        \Magento\Framework\ObjectManagerInterface $objectManager
    ) {
        $this->_request = $request;
        $this->_objectManager = $objectManager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $orderItems = $observer->getOrder();
        $orderItems->getEntityId();
    }
}

I use both conditions one by one but it's not working.

3
  • From your above code how can you tell that it is not working? Have you tried putting logging in the class?
    – Smartie
    Feb 3, 2016 at 12:07
  • @Smartie i am enter order id in my custom module but its not working
    – Newbie
    Feb 5, 2016 at 7:20
  • Where exactly did you put the events.xml?
    – fritzmg
    Jun 9, 2020 at 15:23

4 Answers 4

1

You can use

<event name="checkout_onepage_controller_success_action">
        <observer name="after_place_order" instance="namespace\module\model\Observer"/>
    </event>

in obsereve

<?php
namespace namespace\module\model\Observer;


use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Catalog\Model\ProductRepository;

class Observer implements \Magento\Framework\Event\ObserverInterface {


    protected $orderRepository;
    protected $ProductRepository;
    protected $customerSession;

    public function __construct(  

    OrderRepositoryInterface $OrderRepositoryInterface,
    ProductRepository $ProductRepository,
    \Magento\Customer\Model\Session $customerSession
    ) {
        $this->orderRepository = $OrderRepositoryInterface;
        $this->ProductRepository =$ProductRepository;
        $this->_customerSession = $customerSession;

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

        $order_ids = $observer->getEvent()->getOrderIds()[0];

        $order = $this->orderRepository->get($order_ids);
        foreach ($order->getAllItems() as $item) {
            //code here
            }

}
1
  • hi @sagar Dobariya let me know how can save custom field data here? could u tell me thanks Jun 29, 2017 at 7:12
0

No need to change any event just adds the log in the execution file. 
Like this.

Event.xml

<event name="sales_order_place_after">
    <observer name="oreder_item" instance="Example\Details\Observer\OrderItem" / >
</event>

And your Observer add the log like this. OrderItem.php

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

    $orderId = $order->getId();

    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/testingObserver.log');

    $logger = new \Zend\Log\Logger();

    $logger->addWriter($writer);

    $logger->info($orderId."OrderId");

}

After placing the order please checks your log you will see the order ID.



Kay K
https://www.aspirationhosting.com
0

Can you please try below code.

please create events.xml in etc folder.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_success">
        <observer name="after_order_place" instance="Vender\Module\Observer\OrderPlaceAfter" />
    </event>
</config>

please create OrderPlaceAfter.php class in Observer folder

Path:- Vender\Module\Observer\OrderPlaceAfter.php

<?php

namespace Vender\Module\Observer;


use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class OrderPlaceAfter implements ObserverInterface
{
    
    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        // your code
    }
}
-1

Your way of implementation is wrong. You need to observe the event under your module's config.xml file. As mentioned below,

Under your module etc/config.xml

<frontend>
...
 <sales_order_place_after>
            <observers>
                <mobileapp_clearcart_after_weborder>
                    <type>singleton</type>
                    <class>NAMESPACE_MODULE_Model_Observer</class>
                    <method>getorderDetails</method>
                </mobileapp_clearcart_after_weborder>
            </observers>                
        </sales_order_place_after>
...
</frontend>

Under Model/Observer.php

class NAMESPACE_MODULE_Model_Observer {

    public function getorderDetails($observer){
        $order = $observer->getEvent()->getOrder();     
        $billing = $order->getBillingAddress();
        $shipping = $order->getShippingAddress();
        $customer = $order->getCustomer();         
        $order_id = $order->getIncrementId();
        $customer_id = $order->getCustomerId();         
        return true;
    }

}

Replace NAMESPACE with your Package name and MODULE with your Module name.

Hope this helps.

3
  • Question is about "Magento 2". Are you sure this method is still valid ?
    – Prateek
    Feb 3, 2016 at 12:49
  • @Prateek No it wont. I didn't see the magento2 tag. Feb 3, 2016 at 13:02
  • @Vinoth I am using Magento 1, how to print what event using in observer and print into log-file, just print event in log-file, how can i do that?
    – zus
    Aug 14, 2019 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.