Magento 2 – How to Pass Data from Observer to Phtml File

event-observermagento2ordersphtml

I am getting order id from order success event, I want to pass order into phtml page.
I tried with checkout session but I did not get order id in phtml page.
anyone can help me regarding this.
i am getting order from bellow code

$order_ids = $observer->getEvent()->getOrderIds()[0];
           $order = $this->orderRepository->get($order_ids);
           $orderId  = $order->getIncrementId();

set session and now getting session value in phtml page

$observer_session = $objectManager->get('Magento\Framework\Session\SessionManagerInterface');
$order = $observer_session->getValue();

Best Answer

You need to do like this.

  1. From your observer, set the order value into custom session variable.

  2. On phtml file, get those value from session variable.

Here are the demo code.

protected $_coreSession;

public function __construct(
    -----
    \Magento\Framework\Session\SessionManagerInterface $coreSession
    ){
    $this->_coreSession = $coreSession;
    ----
}

public function setValue(){
    $this->_coreSession->start();
    $this->_coreSession->setMessage('YOUR_CUSTOM_VALUE');
}

public function getValue(){
    $this->_coreSession->start();
    return $this->_coreSession->getMessage();
}

public function unSetValue(){
    $this->_coreSession->start();
    return $this->_coreSession->unsMessage();
}
Related Topic