1

I have used event

<event name="checkout_onepage_controller_success_action">

when I use this code I am getting null $order

        $order = $observer->getEvent()->getOrder();   // im getting null
        $order_id = $order->getIncrementId();
        $this->_logger->info($order_id);
        foreach($order->getAllItems() as $item){
            $ProdustIds[]= $item->getProductId();

            $proName[] = $item->getName(); // product name
        }
        $this->logger->info($ProdustIds);
        $customer = $order->getCustomerId();

how I can get order id and customer name in Magento 2???

2

If you want to get order id on checkout_onepage_controller_success_action then you have to call $observer->getEvent()->getOrderIds()

$observer->getEvent()->getOrderIds() provide an array which consist of one order id of single checkout and provide multiple order ids.

 <?php

nameSpace YourNameSpace;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use usMagento\Framework\Exception\NoSuchEntityException;
class OrderSuccess implements ObserverInterface{

    /**
     * @var \Magento\Sales\Model\OrderRepository
     */
    private $orderRepository;

    public function __construct(
   \Magento\Sales\Model\OrderRepository $orderRepository
   ) {

       $this->orderRepository = $orderRepository;
    }
    public function execute(EventObserver $observer)
    {
        $orderIds = $observer->getEvent()->getOrderIds();
        if (empty($orderIds) || !is_array($orderIds)) {
            return;
        } 
        foreach($orderIds as $orderId){

            try{
                $order = $this->orderRepository->get($orderId);
                if ($order->getCustomerFirstname()) {
                    $customerName = $order->getCustomerFirstname() . ' ' . $order->getCustomerLastname();
                } else {
                    $customerName = $order->getBillingAddress()->getFirstname(). ' ' . $order->getBillingAddress()->getLastname();
                }
                //$customerName = $order->getCustomerName();
            } catch (NoSuchEntityException $ex) {

            }


        }
   }
}
  • but customer name shows as guest...... – prabhakaran7 May 20 at 8:48
  • The answer is updated. I have added a condition, – Amit Bera May 20 at 8:56
0

To get order ID change your code like below :

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

To get Shipping Adress load order:

//\Magento\Sales\Api\OrderRepositoryInterface
$_order =  $this->orderRepository->get($order->getEntityId());

$shippingAddress = $_order->getShippingAddress();
 $shippingName = $shippingAddress->getFirstName().$shippingAddress->getLastName();
  • 1
    Because shipping name can be anythings. You can't say shipping name will be always current customer name. – ravichandra May 20 at 7:28
  • Notice: Undefined property: $order – prabhakaran7 May 20 at 8:36
  • $order->getEntityId() should be your order_id In this case it will be $order_id. – Vivek May 20 at 8:45
0

Its very easy, Please find below code. To get customer name find the code below.

$customer = $observer->getData('data_object');
 if ($customer) {
      $customerName = $customer->getFirstName();
   }

To get the order use the code below:

$observer->getOrder()
  • i'm getting null bro in $customer variaable – prabhakaran7 May 20 at 8:52
0

Try this

$orderId=$observer->getEvent()->getOrderIds();
$order = $this->order->load($orderId);
$orderid=$order->getEntityId();  /* Order id */
$quoteid=$order->getQuoteId();
$customer_first_name=$order->getCustomerFirstname();
$customer_last_name=$order->getCustomerLastname(); /*  customer name */
  • Because shipping name can be anythings. You can't say shipping name will be always current customer name. – ravichandra May 20 at 7:29
  • Notice: Undefined property: $order – prabhakaran7 May 20 at 8:36

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.