-1

This is my code but it gives me error on line 1, $order is not defined.

<?php

   $orderId = $order->getCustomerId();

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

   $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId); 

   //Fetch customer information

   echo $order->getCustomerEmail();
  • can you please share whole code. – Anas Mansuri Sep 18 at 4:56
  • this is whole code in this custom file i have created to get customer email – Junaid Ashfaq Sep 18 at 4:57
  • when i pass static id its give my result but its not take dynamic id – Junaid Ashfaq Sep 18 at 4:58
  • That why he is asking about your whole code....we need what you trying to use to get orders dynamically – Ranganathan Sep 18 at 4:59
  • Please explain in detail. Which type of dynamic order id? – Sweety Masmiya Sep 18 at 5:00
3

Create an after_order_place observer and pest below code in observer :

namespace Company\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\ResourceModel\Order\Address\CollectionFactory;

class OrderSuccess implements ObserverInterface {

    /**
     * @var OrderRepositoryInterface
     */
    protected $orderRepository;

    public function __construct(
        OrderRepositoryInterface $orderRepository
    )
    {
        $this->orderRepository = $orderRepository;
    }

    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {

        $orderIds = $observer->getEvent()->getOrderIds();
    }


}
3

try with below code :

<?php

   $orderId = 1;

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

   $order = $objectManager->create('\Magento\Sales\Model\OrderRepository')->get($orderId); 

   //Fetch customer information

   echo $order->getCustomerEmail();
?>
  • its work for only order id 1 but i want when order take place then email generate and its show my current order id details not only for order 1. – Junaid Ashfaq Sep 18 at 5:05
  • you mean when place order than you get dynamic order id right ? – Anas Mansuri Sep 18 at 5:07
  • yes you are right when place order than i get dynamic order id – Junaid Ashfaq Sep 18 at 5:39

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.