We have a custom section that stores information about our legacy customers. It is necessary to update this information based on a user action. The action triggers an AJAX call which updates a customer attribute and also triggers the section update. However when this event fires, it is logging the customer out. Everything that needs to be updated, is updated. The section gets updated with the correct data and we aren't seeing any errors anywhere. We have looked through everything we can think of but have not been able to find the cause of this. We do know when you call Magento\Customer\Model\Session::isLoggedIn when it calls, $this->storage->getData('xxxxxxxx'); it returns empty, regardless of what data is being retrieved.
etc/frontend/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Customer\CustomerData\SectionPoolInterface"> <arguments> <argument name="sectionSourceMap" xsi:type="array"> <item name="customsection" xsi:type="string">Company\Module\CustomerData\CustomSection</item> </argument> </arguments> </type> </config>
etc/sections.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd"> <action name="company/module/controlleraction"> <section name="customsection"/> </action> </config>
Section.php
<?php namespace Company\Module\CustomerData; use Company\Module\Helper\Data as CustomerHelper; use Magento\Customer\Helper\Session\CurrentCustomer; class CustomSection implements SectionSourceInterface { /** @var CurrentCustomer */ protected $_currentCustomer; /** @var CustomerHelper */ protected $_customerHelper; public function __construct( CurrentCustomer $currentCustomer, CustomerHelper $customerHelper ) { $this->_currentCustomer = $currentCustomer; $this->_customerHelper = $customerHelper; } public function getSectionData() { $result = []; if ($this->_currentCustomer->getCustomerId()): $result = [ 'legacy_account_number' => $this->_customerHelper->getLegacyAccountNumber(); ]; endif; return $result; } }
view/frontend/web/js/view/customsection.js
define([ 'uiComponent', 'Magento_Customer/js/customer-data' ], function (Component, customerData) { 'use strict'; return Component.extend({ /** @inheritdoc */ initialize: function () { this._super(); this.customsection = customerData.get('customsection'); } }); });
AJAX call
$.ajax({ type: "post", url: ajaxUrl, data: {'legacy_account_number' : this.value}, dataType: 'json', beforeSend: function () { $('body').trigger('processStart'); }, success: function() { // This section was intentionally left blank }, error: function (xhr, desc, err) { console.log(xhr); console.log("Details: " + desc + "\nError: " + err); }, complete: function() { $('body').trigger('processStop'); } });
Action to update the attribute:
<?php namespace Company\Module\Controller; use Company\Module\Helper\Data; use Magento\Customer\Model\Session as CustomerSession; use Magento\Framework\App\Action; use Magento\Framework\Controller\Result\JsonFactory; use Psr\Log\LoggerInterface; class ControllerAction extends Action\Action { protected $_jsonFactory; protected $_customerSession; protected $_helper; protected $_logger; public function __construct( Action\Context $context, JsonFactory $jsonFactory, CustomerSession $customerSession, Data $helper, LoggerInterface $logger ) { parent::__construct($context); $this->_customerSession = $customerSession; $this->_jsonFactory = $jsonFactory; $this->_helper = $helper; $this->_logger = $logger; } public function execute() { $legacyAccountNumber = $this->getRequest()->getParam('legacy_account_number'); if ( ! $legacyAccountNumber): return $this->_jsonFactory->create()->setData(['error' => 'No legacy account number provided.']); endif; /** @var \Magento\Customer\Model\Customer $customer */ $customer = $this->_customerSession->getCustomer(); /** @var \Magento\Customer\Api\Data\CustomerInterface $customerData */ $customerData = $customer->getDataModel(); try { $altAccountNumber = $this->_helper->getAltAccountNumber($legacyAccountNumber); $customerData->setCustomAttribute('active_legacy_account', $altAccountNumber); $customer->updateData($customerData); $customer->save(); $this->_messageManager->addSuccessMessage("Account changed to $altAccountNumber"); } catch (\Exception $e) { $this->_logger->info($e->getMessage()); return $this->_jsonFactory->create()->setData(['error' => $e->getMessage()]); } return $this->_jsonFactory->create()->setData(['active_legacy_account' => $altAccountNumber]); } }
I'm not sure if we are missing something or doing something incorrectly, but this is the base of the code in question. Please ask questions if you have any.
Thanks
We found the issue today, we were saving/updating the attribute incorrectly.