12

I'm implementing a custom store switcher in my custom theme.

Block template is as follow:

<?php if (count($block->getStores())>1): ?>
    <?php $id = $block->getIdModifier() ? '-' . $block->getIdModifier() : ''?>

    <span><?php echo $block->escapeHtml($block->getStoreName()) ?></span>

    <span>/</span>

    <?php foreach ($block->getStores() as $_lang): ?>
        <?php if ($_lang->getId() != $block->getCurrentStoreId()): ?>
            <span class="view-<?php echo $block->escapeHtml($_lang->getCode()); ?> switcher-option">
                <a href="#" data-post='<?php /* @escapeNotVerified */ echo $block->getTargetStorePostData($_lang); ?>'>
                    <?php echo $block->escapeHtml($_lang->getName()) ?>
                </a>
            </span>
        <?php endif; ?>
    <?php endforeach; ?>

<?php endif; ?>

The link to new store is correctly displayed so I guess the xml stuff is OK, but on click I receive the following error:

section-config.js:33 Uncaught TypeError: Cannot read property '*' of undefined(…)

The generated html looks like the following:

<span class="view-it switcher-option">
  <a href="#" data-post="{"action":"http:\/\/test.mysite.eu\/stores\/store\/switch\/","data":{"___store":"it","uenc":"aHR0cDovL3Rlc3QuZnJhZ2lhY29tb21pbGFuby5ldS8,"}}">
    It
  </a>
</span>
9
  • Have you run deploy command and remove var folder? Nov 30, 2016 at 9:36
  • nope XD I'll do it and follow up
    – davideghz
    Nov 30, 2016 at 9:36
  • I just ran sudo php bin/magento setup:static-content:deploy w/o any luck. Why shall I remove var folder? What exactly should I remove?
    – davideghz
    Nov 30, 2016 at 9:40
  • you have to remove all content of var folder Nov 30, 2016 at 9:41
  • 1
    @davideghz did you get the solution ? Feb 28, 2017 at 10:01

5 Answers 5

16

I had the same problem and found out that these two blocks where not called any more due to customizations in the theme.

The two blocks are per default added in the <referenceContainer name="content"> of vendor/magento/module-customer/view/frontend/layout/default.xml.

Try re-adding these blocks in your layout.xml:

<block name="customer.section.config" class="Magento\Customer\Block\SectionConfig"
    template="Magento_Customer::js/section-config.phtml"/>
<block name="customer.customer.data"
    class="Magento\Customer\Block\CustomerData"
    template="Magento_Customer::js/customer-data.phtml"/>
2
  • I have the same Problem but the Blocks are rendered. I guess it's because I am in a requireJs method while calling the cart reload method, but the JS is executed before the block is rendered. Can I depend on any js module to avoid this?
    – bpoiss
    Dec 1, 2017 at 8:00
  • 1
    Just had this exact same problem.. while using github.com/SnowdogApps/magento2-theme-blank-sass (Magento 2.2.6). Adding the two above blocks fixed the problem. Thanks Nov 3, 2018 at 21:57
8

On our project we fixed it in the section-config.js file in the method getAffectedSections (which is where our error was).
We replaced the last line:

return _.union(_.toArray(actions), _.toArray(sections['*']));

By the following one:

return (sections && sections.length > 0) ? _.union(_.toArray(actions), _.toArray(sections['*'])) : {};

To actually returns an empty array when no section is affected.

6
  • Does your solution work well?
    – Jared Chu
    Dec 17, 2018 at 3:58
  • 1
    Yes it was a functionning workaround, but IIRC, the real root of our issue was a slight misuse of the JS API in the end. It did not make this fix unrelevant, it's still a protection to ensures the code keeps running and does not stop here, in not critical error cases.
    – Flinth
    Dec 18, 2018 at 14:38
  • 1
    Thanks, It's working..
    – Ravi Soni
    May 22, 2019 at 6:54
  • It is working for me also. But in some categories when I change store view, after store view is changed I get invalid form key. Please refresh page. No form is there though....I am confused
    – G. G.
    Jan 25, 2020 at 22:56
  • 1
    Working fine :) thanks you very much. Jun 7, 2020 at 18:12
0

Ran across the same issue even though the customer.section.config and customer.customer.data were properly loaded into the page.

In my case however it happened because an extension tried to trigger a section-reload too early (before section-config.js was loaded with the right configuration).

Managed to solve it by adding a breakpoint in getAffectedSections of section-config.js and checking the url variable. Then moved the extension JS to load after the customer.section.config block:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <move element="extension.js.block" destination="content" after="customer.section.config"/> 
    </body>
</page>
0

In our case this was a bit of code in a default template which was loading the customer-data.js file after it had already been called on the success page.

We found file: vendor/magento/module-checkout/view/frontend/templates/success.phtml

was loading this code:

<script>
    require([
        'Magento_Customer/js/customer-data'
    ], function (customerData) {
        var sections = ['cart'];
        customerData.invalidate(sections);
        customerData.reload(sections, true);
    });
</script>

Removing this code fixed it, which we did by creating a copy of this file in our theme templates directory and removing the offending code snippet.

You can use Chrome Dev tools to find out where the error was happening and then just before in that file on your local dev add in a console.trace(); call to establish what was calling the function which is throwing the error in the console. Follow the chain to where you are loading things improperly.

0

In my case, someone has removed the "columns" container like below from the custom page layout

<referenceContainer name="columns" remove="true" />

Removing this line makes it working and the error is gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.