2

When you login to Magento 2.2.6 programmatically with code below, header show default message "Default welcome msg!" instead of "Welcome, user!"

// Load customer session
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setCustomerAsLoggedIn($customer);

Expected result

  • Session should get started
  • Header should show "Welcome, user!"

Actual result

  • Session getting started
  • Header shows "Default welcome msg!"

Unlike when you login with magento login page, it is not picking customer fullname during login with code.

1 Answer 1

0

The welcome message on header using customer data scope.

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Theme/view/frontend/templates/html/header.phtml

You'd need to trigger invalidate for customerData after your action

customerData.invalidate(['customer']);
customerData.reload(['customer'], true);

For example

  • Event trigger

    $(document).on('ajaxComplete', function (event, xhr, settings) {
        var sections,
            redirects;
    
        if (settings.type.match(/post|put/i)) {
            sections = sectionConfig.getAffectedSections(settings.url);
    
            if (sections) {
                customerData.invalidate(sections);
                redirects = ['redirect', 'backUrl'];
    
                if (_.isObject(xhr.responseJSON) && !_.isEmpty(_.pick(xhr.responseJSON, redirects))) {
                    return;
                }
                customerData.reload(sections, true);
            }
        }
    });
    
  • Use section sections.xml file to etc/frontend for controller/action

     <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="braintree/paypal/placeOrder">
        <section name="cart"/>
        <section name="checkout-data"/>
    </action>
    </config>
    
0

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.