0

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!

3
  • 1
    I don't have enough specifics to really defend this point but it sounds like you're simply going the wrong direction here. Its rather unusual to start creating sessions and set sessions IDs and whatnot. Whats the problem you're trying to solve? Jul 13, 2021 at 3:36
  • specify your actually requirement, so we can suggest the solution accordingly. Jul 13, 2021 at 5:22
  • hi, my requirement is to simply get a value (uniqueid) when a form loads. Because the customer is redirect from an application to my magento store, so in that moment I need to catch the UNIQUEID and then take that value to the checkout, so I can save it on Order's table. Jul 14, 2021 at 15:14

1 Answer 1

0

You can save data to Magento's default session without creating a separate session

protected $_checkoutSession;
public function __construct(
    ....
    public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession
) {

    $this->_checkoutSession = $checkoutSession;
}

public function getCheckoutSession() 
{
    return $this->_checkoutSession;
}

You set a value : $this->getCheckoutSession()->setVarValue('someValueHere');

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.