2

I'm trying to call a Magento Javascript Component in my phtml page to get the customer data saved in session.

I'm having an issue in that the customerdata constructor gets loaded after my script and I can't figure out how to get my script to run after.

This is the customer-data.js file that needs to get loaded before my script

'Magento_Customer/js/customer-data': function (settings) {
            options = settings;
            invalidateCacheBySessionTimeOut(settings);
            invalidateCacheByCloseCookieSession();
            customerData.init();
 }

Here is how I'm trying to call it

require([
    'jquery',
    'Magento_Customer/js/customer-data',
    'domReady!',
    'mage/loader'
], function (jQuery,customerData) {
    //<![CDATA[
    jQuery(document).ready(function() {

        var data = customerData.get('mysession-data');


    });
});

My code works when the customerdata gets initialized but it's not always getting initialized first.

Is there anyway to configure this to get the customerdata to initialize and then I can call my session data?

1
  • have you find a way to resolve this case?
    – LucScu
    Sep 22, 2017 at 10:42

2 Answers 2

1

You can try to use RequireJs mixins in order to hook the core function.

var config = {
    'config':{
        'mixins': {
            'Magento_Customer/js/customer-data': {
                'Vendor_Module/hook.js':true
            }
        }
    }
}; 

Alan Storm described it here : http://alanstorm.com/the-curious-case-of-magento-2-mixins/

You can try to use shim option with requirejs as explain here :

Requirejs shim option not working

2
  • thanks. I don't really want to alter an existing function. I just want to get the content of customerData after it's initialized. I think the script that runs the "text/x-magento-init" must happen after my code
    – jstrez77
    Jan 3, 2017 at 4:07
  • Do you try shim option ? The jQuery ready is not require I think. Jan 4, 2017 at 7:06
0

Add this in

// Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0" >
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

This basically tells magento to load Magento_Customer module first before running your module, so by then customer-data will be available.

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.