0

I'm trying to get the CustomerId when an order is placed so that I can manipulate it before the order is saved to the DB.

Vendor/Module/etc/frontend/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="sales_order_place_after">
        <observer name="VendorModule" instance="Vendor\Module\Observer\Order\Order"/>
    </event>
</config>

Vendor/Module/Observer/Order/Order.php

<?php

declare(strict_types=1);

namespace Vendor\Module\Observer\Order;

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

class Order implements ObserverInterface
{
    protected $_logger;
    protected $order;

    /**
     * Order constructor.
     * @param \Psr\Log\LoggerInterface $_logger
     * @var \Magento\Sales\Model\Order $order
     */

    public function __construct(
        \Psr\Log\LoggerInterface $_logger,
        \Magento\Sales\Model\Order $order
    )
    {
        $this->logger = $_logger;
        $this->order = $order;
    }

    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        $customerId = $order->getCustomerId();
        $this->_logger->info('Customer ID is' . $customerId);

        if ($customerId !== 0) {
            //do stuff
        }
    }
}

This does not even log anything to var/log/debug.log How would I get the customerId on sales_order_place_after observer?

UPDATE

<?php

declare(strict_types=1);

namespace Vendor\Module\Observer\Order;

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

class Order implements ObserverInterface
{
    private $logger;
    private $order;
    private $customer;

    /**
     * Order constructor.
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magento\Sales\Model\Order $order
     * @param \Magento\Customer\Model\Session $customer
     *
     */

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Sales\Model\Order $order,
        \Magento\Customer\Model\Session $customer
    )
    {
        $this->logger = $logger;
        $this->order = $order;
        $this->customer = $customer;
    }

    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getOrder();
        //$customerId = $order->getCustomerId();
        $customer = $this->customer;
        $customerName = $customer->getName();
        $this->logger->info('Customer name is' . $customerName);

        if ($customerName !== "Tom") {
            die();
        }
    }
}
3
  • I am not sure that sales_order_place_after is running for your case.The code is ok seems from end
    – Amit Bera
    Commented Feb 17, 2021 at 10:08
  • Have you checked error log? Is website in developer mode? Can you please write die(); in the starting of execute function? And can you please try to place order again?
    – ZealousWeb
    Commented Feb 18, 2021 at 14:01
  • @ZealousWeb Even if I write die(); in the starting of the function, I can still place orders. That's why I think sales_order_place_after isn't called at all. Could this be a bug in Magento 2.4.2? Could you please try if you can call the event at all? Thanks Commented Feb 18, 2021 at 17:13

1 Answer 1

0

I would suggest you take the customer ID from session. In the same observer class, inject Magento\Customer\Model\Session into the constructor and save it in a private property, say $session and use it like this:

$this->session->getCustomerId()

Sidenotes

  • you don't need to initialize the order object in the constructor in your above code
  • your observer's properties should be private and naming without 'underscore' as per PSR-2
4
  • Thanks, could you please check the update. It seems like sales_order_place_after does not run at all after placing an order because it does not do anything. Commented Feb 17, 2021 at 10:38
  • Try changing the event into checkout_submit_all_after.
    – Diana
    Commented Feb 17, 2021 at 10:59
  • Nope, not working. Commented Feb 17, 2021 at 11:52
  • maybe redundant, but please make sure that your debug logging settings are enabled in admin, correct permissions are set and i would suggest you use a debugger and see if indeed it doesn't 'enter' your observer.
    – Diana
    Commented Feb 17, 2021 at 11:54

Your Answer

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

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