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>
  • Have you run deploy command and remove var folder? – Rakesh Jesadiya Nov 30 '16 at 9:36
  • nope XD I'll do it and follow up – davideghz Nov 30 '16 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 '16 at 9:40
  • you have to remove all content of var folder – Rakesh Jesadiya Nov 30 '16 at 9:41
  • 1
    @davideghz did you get the solution ? – Mohammad Mujassam Feb 28 '17 at 10:01
14

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"/>
  • 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 '17 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 – giolliano sulit Nov 3 '18 at 21:57
4

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.

  • Does your solution work well? – Jared Chu Dec 17 '18 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 '18 at 14:38
  • Thanks, It's working.. – Ravi Soni May 22 at 6:54
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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