I am currently developing a PrestaShop module that allows users to switch the catalog mode on or off depending on whether they are logged in or not.
Everything is working well, but I have encountered an issue.
I want unlogged users to not be able to see prices or make orders. However, with the current solution I have implemented, when an unlogged user first loads the page (with the catalog mode off), the catalog mode turns on and they can see prices (they have to reload the page to hide the prices). So, the first load sets the catalog mode on and the second load displays the real catalog mode.
I have found a JavaScript script that auto-reloads the page to apply the new mode, but this results in the page loading time being twice as long.
Here is the function:
public function hookHeader()
{
$logged = $this->context->customer->isLogged();
if (!$logged) {
Configuration::updateValue('PS_CATALOG_MODE', true);
} else {
Configuration::updateValue('PS_CATALOG_MODE', false);
}
// reload the page once more
echo '
<script type="text/javascript">
(function() {
if( window.localStorage ) {
if( !localStorage.getItem( "firstLoad" ) ) {
localStorage[ "firstLoad" ] = true;
window.location.reload();
} else {
localStorage.removeItem( "firstLoad" );
}
}
})();
</script>
';
}
I hope someone can assist me with resolving this issue. Thank you.