1

Hi i'm new at magento 2 and want to get orderId() using Observer on place order event.

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <event name="sales_order_place_after">
        <observer name="place_order" instance="SussexDev\Extrato\Observers\Observers\PlaceOrder" shared="false" disabled="false"/>
    </event>
</config>

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

class PlaceOrder implements ObserverInterface
{
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }
public function execute(Observer $observer)
{
    //$request = $observer->getEvent()->getOrder();
    //echo "Request id: ".$request->getId();
    $txt = "\n aaaaaa";
    $this->_logger->log('DEBUG', $txt);
    $this->logger->info('Observer Atingido2', $observer->debug());

}

}

I got the conclusion that i'm Observing the wrong event, i want to observer the event that happens when this button (Place Order) get clicked.

event

I realized that my problem is other, the Observer isn't called and i don't know why. I created another post please check here: My Observer is not executing

1

Check this

On checkout_submit_all_after event: This event is dispatched Place Order is clicked.

File:-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="checkout_submit_all_after">
        <observer name="place_order" instance="SussexDev\Extrato\Observers\PlaceOrder"/>
    </event>

</config>

Observer Code

$order = $observer->getEvent()->getOrder();
$incrementId = $order->getIncrementId();

OR

$order = $observer->getEvent()->getOrder();
$orderId = $order->getId();
| improve this answer | |
2

On sales_order_place_after event: This event is dispatched after the order payment is placed.

$order = $observer->getEvent()->getOrder();
$incrementId = $order->getIncrementId();

On sales_order_save_after event: This event is called every time the order is saved. For eg. Order status changed, invoiced, shipped & credit memo.

$order = $observer->getEvent()->getOrder();
$orderId = $order->getId();

Update based on @Rafael's comment: You should replace:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

with

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

The line you have written in events.xml is for module.xml file.

| improve this answer | |
  • thank you for the answer, i changed the event for sales_order_save_after, but the observer still not executing. – Rafael Fagundes May 29 at 14:28
  • As I could see, your events.xml file is wrong. Please correct it as mentioned in my updated answer. – Mohit Kumar Arora May 29 at 14:36
  • Thank you for correcting the events.xml. But i got the conclusion that i'm Observing the wrong event, i want to observer the event that happens when this button (Place Order) get clicked. – Rafael Fagundes May 29 at 14:56
  • I realized that my problem is other, the Observer isn't called and i don't know why. I created another post please check here: magento.stackexchange.com/questions/313953/… – Rafael Fagundes May 29 at 17:17

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.