Unable de reload customer data section
-
08-02-2021 - |
Question
I discover that some native route are listed in the section xml file without any section :
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
<action name="customer/account/logout"/>
<action name="customer/account/loginPost"/>
<action name="customer/account/createPost"/>
<action name="customer/account/editPost"/>
<action name="customer/ajax/login">
<section name="checkout-data"/>
<section name="cart"/>
</action>
</config>
In this case the SectionConfigConverter.php will add '*' to signify all sections need to be reloaded. But when one of this route are used, magento try to reload this sections and I got :
"*" section source is not supported
It doesn't seem that the native code is able to deal with the '*' it has added itself. Have you already encounter/resolve this bug ?
Thank you in advance
Solution
I can't understand why this is not working out of the box, but here is workaround for issue:
Plugin which makes this job. In case the *
symbol was found in list of section names it makes the section name equals null
which means for the Magento\Customer\CustomerData\SectionPool
class to update all the sections.
etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- Plugins -->
<type name="Magento\Customer\CustomerData\SectionPool">
<plugin name="workaround_update_all_sections"
type="MageWorx\CustomerDataFix\Plugin\UpdateAllCustomerDataSections"
sortOrder="10"
disabled="false"/>
</type>
</config>
app/code/MageWorx/CustomerDataFix/Plugin/UpdateAllCustomerDataSections.php
<?php
/**
* Copyright © MageWorx. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace MageWorx\CustomerDataFix\Plugin;
use Magento\Customer\CustomerData\SectionPool;
/**
* Class UpdateAllCustomerDataSections
*
* Update all sections in case the wildcard symbol was found in the sections-to-update array
*/
class UpdateAllCustomerDataSections
{
/**
* @param SectionPool $subject
* @param array|null $sectionNames
* @param bool $updateIds
* @return array
*/
public function beforeGetSectionsData(
SectionPool $subject,
array $sectionNames = null,
$updateIds = false
) {
// Trying to find a wildcard in the "sections-to-update" array
if (!empty($sectionNames) && array_search('*', $sectionNames) !== false) {
// If found drop all section names to allow update of all sections
$sectionNames = null;
}
return [$sectionNames, $updateIds];
}
}
Complete code (module) could be found here on GitHub.
I found a strange code and I think that it should be responsible for this:
Magento_Customer/js/section-config.js
getAffectedSections: function (url) {
var route = canonize(url),
actions = _.find(sections, function (val, section) {
var matched;
if (section.indexOf('*') >= 0) {
section = section.replace(/\*/g, '[^/]+') + '$';
matched = route.match(section);
return matched && matched[0] == route; //eslint-disable-line eqeqeq
}
return route.indexOf(section) === 0;
});
return _.union(_.toArray(actions), _.toArray(sections['*']));
},
but a route could match the * section only in case it has no /
. I don't know how it could be achieved in real world :\ Maybe it could be useful for you somehow.