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?

asked Dec 31, 2016 at 16:50
1
  • have you find a way to resolve this case? Commented 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

answered Jan 2, 2017 at 7:20
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 Commented Jan 3, 2017 at 4:07
  • Do you try shim option ? The jQuery ready is not require I think. Commented 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.

answered Feb 5, 2018 at 0:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.