1

I am following this in order to create new section https://developer.adobe.com/commerce/php/development/cache/page/private-content/

All is working if I request '{localhost}/customer/section/load', the custom section 'badges'with data appears there

enter image description here

Issue is with accessing it using KnockoutJS component. My code is following:

define([
    'jquery',
    'uiComponent',
    'Magento_Customer/js/customer-data',
    'ko'
], function ($, Component, customerData, ko) {
    return Component.extend({
        defaults: {
            template: 'Vendor_Module/template',
        },

        initialize: function () {
            this._super();
            this.badges = customerData.get('badges')(); // Returns undefined
            customerData.get('badges').subscribe((badges) => {
                console.log('run badges, run'); // Doesn't executes
                this.badges = badges;
            });
        },

        someAction: function () {
            console.log(this.badges);
        }
    });
});

1 Answer 1

1

can you please try with this

define([
    'uiComponent',
    'Magento_Customer/js/customer-data'
], function (Component, customerData) {
    'use strict';
     var sections = ['badges'];
    customerData.invalidate(sections);
    customerData.reload(sections, true);
    return Component.extend({
        initialize: function () {
            this._super();
            this.badges = customerData.get('badges');
        }
    });
});
1
  • Thank you! I reloaded the customer data customerData.reload(['badges'], true); before accessing it and it seems to work. Oct 22, 2022 at 0:06

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.