Showing results for 
Search instead for 
Did you mean: 

Updating Section Data Logs Customer Out

Updating Section Data Logs Customer Out

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.



<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\CustomerData\SectionPoolInterface">
            <argument name="sectionSourceMap" xsi:type="array">
                <item name="customsection" xsi:type="string">Company\Module\CustomerData\CustomSection</item>



<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="company/module/controlleraction">
        <section name="customsection"/>



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();

        return $result;



], function (Component, customerData) {
    'use strict';

    return Component.extend({
        /** @inheritdoc */
        initialize: function () {

            this.customsection = customerData.get('customsection');


AJAX call

    type: "post",
    url: ajaxUrl,
    data: {'legacy_account_number' : this.value},
    dataType: 'json',
    beforeSend: function () {
    success: function() {
        // This section was intentionally left blank
    error: function (xhr, desc, err) {
        console.log("Details: " + desc + "\nError: " + err);
    complete: function() {


Action to update the attribute:

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
    ) {

        $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.']);

        /** @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);

            $this->_messageManager->addSuccessMessage("Account changed to $altAccountNumber");
        } catch (\Exception $e) {

            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.




Re: Updating Section Data Logs Customer Out

We found the issue today, we were saving/updating the attribute incorrectly.