I need to call some js in a phtml file only on product details page. I need to check some customer attribute there and show a popup accordingly.

The issue I am facing is that somehow I am unable to get details from customerData on page load and it seems as if my js is loaded before the page loads.

I added setTimeout and after that everything works perfectly.


<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceContainer name="before.body.end">
        <block name="trail.user.info" template="Test_TrialuserRestriction::trialuserinfo.phtml" />


<script type="text/javascript">
require(['jquery', 'domReady!', 'Magento_Customer/js/customer-data'], function($, domready, customerData){
    'use strict';

     // Uncommenting this not works for me
    // var customer = customerData.get('customer');
    // console.log(customer().firstname);

    //This works for me when I add timeout
        var customer = customerData.get('customer');
    }, 5000);



Also tried including the js file using require-config.js but still same.

Any help please.

  • Also tried using require js but still same. Sep 19, 2019 at 11:59

1 Answer 1


If anyone is in need of a better and working solution, here you go.

Magento2 RequireJS CustomerData is empty at page load, but populated once page finishes loading?


  • This solution is exactly the same as your (and mine) not working solution. Nov 23, 2019 at 20:07

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.