I have been struggling with this issue like 2 days, and I don't understand why it's happening.
This is what I'm doing:
1- Unset session variable in case it exists and Create a new session variable 2- Add value to session variable 3- get the session variable on Checkout
The 3 step is not working, sometimes it works sometimes not, don't know why.
Here is my code:
Helper of session variables:
<?php
namespace Vendor\Application\Helper;
class SessionUniqueId extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $_coreSession;
public function __construct(
\Magento\Checkout\Model\Session $coreSession
) {
$this->_coreSession = $coreSession;
}
public function startSession($uniqueId){
$this->_coreSession->start();
$this->_coreSession->setUniqueid($uniqueId);
}
public function getSession(){
$this->_coreSession->start();
return $this->_coreSession->getUniqueid();
}
public function deleteSession(){
$this->_coreSession->start();
return $this->_coreSession->unsUniqueid();
}
}
1- Unset and set new variable session
public function setSession($uniqueid){
$this->_sessionHelper->startSession($uniqueid);
$this->_logger->info("Empieza la sesión de ".$uniqueid);
}
public function unsetSession(){
$this->_logger->info("Destruyo la sesión en el block ");
return $this->_sessionHelper->deleteSession();
}
2- I call this 2 methods on a template (phtml) this part works fine
$params = $block->getData('params');
$uniqueid = $block->serializeData($params['uniqueId']);
$block->unsetSession();
$block->setSession($params['uniqueId']);
3- I try to get uniqueId variable session on this event: sales_order_place_after, by calling my helper
public function execute(\Magento\Framework\Event\Observer $observer){
//Get UNIQUE ID of term employee
$uniqueId = $this->_sessionHelper->getSession();
//Get Order data
$order = $observer->getOrder();
$orderNumber = $order->getIncrementId();
$orderGrandTotal = $order->getGrandTotal();
$this->_logger->info("Order Number: ".$orderNumber);
$this->_logger->info("Order Grand Total: ".$orderGrandTotal);
$this->_logger->info("Unique ID: ".$uniqueId); //here is empty [][]
$this->_sessionHelper->deleteSession();
}
It's practically empty sometimes, when I place an order.
What could it be?
Thanks!