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\Sessionand 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.phpfile, as directed here, but got the same empty session issue.Changed Session storage to Files in my
app/etc/env.phpfile and I got the correct data as expected.
Can I have a solution for this problem?
1 Answer 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;
}
}
-
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– Jehangir Wahid2019年02月25日 16:58:05 +00:00Commented Feb 25, 2019 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-idQaisar Satti– Qaisar Satti2019年03月03日 17:12:04 +00:00Commented Mar 3, 2019 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– Jehangir Wahid2019年03月04日 05:20:57 +00:00Commented Mar 4, 2019 at 5:20
-
Can you share code where you set the session?Qaisar Satti– Qaisar Satti2019年03月04日 18:02:19 +00:00Commented Mar 4, 2019 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– Jehangir Wahid2019年03月05日 05:23:40 +00:00Commented Mar 5, 2019 at 5:23
Explore related questions
See similar questions with these tags.