cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Section (local storage) Issue

SOLVED

Custom Section (local storage) Issue

We created a custom section (for local storage) to store some data that we need for the customer.  We can see that our data is being populated in our section of the local storage, however we are having an issue retrieving the data via Knockout.  This issue only occurs on the home page (cms/index/index), everywhere else the local storage data is readily available.  We can retrieve other sections from local storage on the home page, just not our section.  We have gone through everything we can but can't find the reason for the issue, so we are here to get some ideas.

 

We are running Magento EE 2.1.12.

 

We created a custom module for the section that consists of 3 files (not including etc/module.xml, composer.json or registration.php).

 

etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\CustomerData\SectionPoolInterface">
        <arguments>
            <argument name="sectionSourceMap" xsi:type="array">
                <item name="customsection" xsi:type="string">Company\Module\CustomerData\CustomSection</item>
            </argument>
        </arguments>
    </type>
</config>

Section.php

<?php
namespace Company\Module\CustomerData;


use Company\Module\Helper\Data as CustomerHelper;
use Magento\Customer\Helper\Session\CurrentCustomer;

class CustomSection implements SectionSourceInterface
{
    /** @var CurrentCustomer */
    protected $_currentCustomer;
    
    /** @var CustomerHelper */
    protected $_customerHelper;


    public function __construct(
        CurrentCustomer $currentCustomer,
        CustomerHelper $customerHelper
    ) {
        $this->_currentCustomer = $currentCustomer;
        $this->_customerHelper = $customerHelper;
    }

    public function getSectionData()
    {
        $result = [];
        if ($this->_currentCustomer->getCustomerId()):
            $result = [
                    'legacy_account_number' => $this->_customerHelper->getLegacyAccountNumber();
            ];
        endif;

        return $result;
    }
}

view/frontend/web/js/view/customsection.js

define([
    'uiComponent',
    'Magento_Customer/js/customer-data'
], function (Component, customerData) {
    'use strict';

    return Component.extend({
        /** @inheritdoc */
        initialize: function () {
            this._super();

            this.customsection = customerData.get('customsection');
        }
    });
});

Example of use:

<!-- ko scope: 'customsection' -->
    <!-- ko if: customsection().legacy_account_number > 0 -->
<div>... special stuff for legacy account holders ...</div> <!-- /ko --> <!-- /ko --> <script type="text/x-magento-init"> { "*": { "Magento_Ui/js/core/app": { "components": { "customsection": { "component": "Company_Module/js/view/customsection" } } } } } </script>

This is the base of our code in question, everything works fine with this code except on the home page. Any suggestions or advice would be greatly appreciated.

6 REPLIES

Re: Custom Section (local storage) Issue

Hello @jrose_ewing1

 

you did great stuff.

 

There is one functionality EE banner functionality

 

It will find under Content >> Banner 

 

Hope it will help you.

 

If it will help you then mark as solutionn

Re: Custom Section (local storage) Issue

Hey @Sunil Patel

 

Thank you very much for your quick response, however I do not think it will work in our case.  My understand of banners is that they are used to create promotions and such, which isn't what we are trying to do.  I should have elaborated a little more on what we are using the data from the local storage section for, as I only posted a condensed version of our code.  We use this data to get custom pricing on items for our "legacy" customers.  The custom pricing uses the value in the local storage to retrieve customer specific pricing, so using something like a customer segment in a banner will not work in this case.

 

Thank you again.

Re: Custom Section (local storage) Issue

Hello @jrose_ewing1

 

I don't your purpose behind the code so I suggest that way.

 

If you want to assign pricing for customer wise then assign such customer to specific group and create tier price for same.

 

Hope it will help you.

 

If work then mark as solution

Re: Custom Section (local storage) Issue

This will not work either, our companies pricing structure is too complex for Magento's tiered pricing.

 

I appreciate your help and suggestions, however they are not addressing my issue, they are, what I would consider, work arounds.  The way we are doing our price and the code we are using should work fine, and for every page except the home page, it works fine, so we need to figure out why the code doesn't work on the home page.

 

We did discover through debugging KnockOut that our data is populated and visible to a certain point, then it gets cleared out, we can't figure out why it is being cleared out, but it is.

Re: Custom Section (local storage) Issue

We finally discovered the issue, we had a template that was double scoped for KnockOut.

Highlighted

Re: Custom Section (local storage) Issue

@jrose_ewing1

 

in your template file there is only one scope