1

I am facing a problem in getting session data in Magento 2, when I deploy my Build on 2 Web-Servers. I am facing no issue on Single Web-Server.

Problem Details I am setting some data in session, in first AJAX request, and then getting that data in the next consecutive AJAX request but the session returns no values.

AJAX-Request Format:

jQuery.ajax({
    url: myUrl,
    type: 'POST',
    dataType: 'json',
    data: myData,
    cache: false,
})

Problem Resolution Approaches

  • Printed overall session data and found empty customer data. Only the following data is returned.

    Array([customer_segment_ids] => Array([1] => Array([0] => 3))[wishlist_item_count] => 0)

  • Tried to load session data using ObjectManager, while created object of \Magento\Customer\Model\Session and provided the SessionID to get data from the Database-Table using my custom code, but it returned empty array.

  • Tried to load session data using MySQL Custom Query by SessionID on that table and got data.

  • Changed Session storage to Redis in my app/etc/env.php file, as directed here, but got the same empty session issue.

  • Changed Session storage to Files in my app/etc/env.php file and I got the correct data as expected.


Can I have a solution for this problem?

1

I use the following code to get customer data with the cache enabled which I can achieve by the following code. But you are using the ajax controller request so for you cache will be no problem. kindly share your full code here.

namespace QaisarSatti\Module\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{

  protected $objectmanager;  


  public function __construct(

        \Magento\Framework\ObjectManagerInterface $objectmanager

    ) {


        $this->objectmanager = $objectmanager;

    }
    public function getCustomerData()
    {
         $customerSession = $this->objectmanager->create('Magento\Customer\Model\SessionFactory')->create();
         return $customerSession;
    }

}

Reference

  • Thank you for your consideration, There is nothing special about my code, I used the conventional way to get data from the Customer-Session, as follows; namespace MyNamespace\MyModule\Controller\Area; use Magento\Customer\Model\Session; class MyController extends \Magento\Framework\App\Action\Action { protected $_customerCollection; public function __construct(Session $customerSession) { $this->_customerSession = $customerSession; } public function execute() { $myData = $this->_customerSession->getMyCustomData(); } } – Jehangir Wahid Feb 25 at 16:58
  • @JehangirWahid if you want to get customer attribute then load through the model. follow blog.qaisarsatti.com/magento_2/magento2-load-customer-by-id – Qaisar Satti Mar 3 at 17:12
  • No Sir, I am not trying to get Customer Attributes. I am setting some custom data in session the first client request, and then am trying to get that data in the next consecutive request. – Jehangir Wahid Mar 4 at 5:20
  • Can you share code where you set the session? – Qaisar Satti Mar 4 at 18:02
  • Yes, for sure. namespace MyNamespace\MyModule\Controller\Area; use Magento\Customer\Model\Session; class MyController extends \Magento\Framework\App\Action\Action { protected $_customerCollection; public function __construct(Session $customerSession) { $this->_customerSession = $customerSession; } public function execute() { // $myData is an array $this->_customerSession->setMyCustomData($myData); } } – Jehangir Wahid Mar 5 at 5:23

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.