Javascript 如果用户未标记/记录,则预设打开/关闭目录模式
我正在使用一个prestashop模块,在用户未被跟踪或登录时,将目录模式设置为打开或关闭 效果很好,但遇到了问题 我不想让未登录的用户看到价格并允许他们订购。但在我找到的解决方案中,当第一个连接(modecatalog OFF)卸载用户加载页面时,catalog mod打开,但他可以看到价格(必须重新加载才能隐藏价格),因此,第一个加载设置catalog mode打开,第二个加载显示真实的catalog mode 我发现一个js脚本可以自动重新加载以使新模式生效,但很明显,页面的加载时间要长两倍 以下是函数:Javascript 如果用户未标记/记录,则预设打开/关闭目录模式,javascript,module,prestashop,prestashop-1.6,Javascript,Module,Prestashop,Prestashop 1.6,我正在使用一个prestashop模块,在用户未被跟踪或登录时,将目录模式设置为打开或关闭 效果很好,但遇到了问题 我不想让未登录的用户看到价格并允许他们订购。但在我找到的解决方案中,当第一个连接(modecatalog OFF)卸载用户加载页面时,catalog mod打开,但他可以看到价格(必须重新加载才能隐藏价格),因此,第一个加载设置catalog mode打开,第二个加载显示真实的catalog mode 我发现一个js脚本可以自动重新加载以使新模式生效,但很明显,页面的加载时间要长两
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>
';
}
公共函数hookHeader()
{
$logged=$this->context->customer->isLogged();
如果(!$logged){
配置::updateValue('PS\u目录\u模式',true);
}否则{
配置::updateValue('PS\u目录\u模式',false);
}
//重新加载页面
回声'
(功能(){
if(window.localStorage){
如果(!localStorage.getItem(“firstLoad”)){
localStorage[“firstLoad”]=true;
window.location.reload();
}否则{
localStorage.removietem(“firstLoad”);
}
}
})();
';
}
希望有人能帮我。多谢各位 您的解决方案有问题。 您正在更新数据库中的值:如果多个用户正在浏览该站点,该值将被打开/关闭/打开/关闭/…,换句话说,它是“不稳定的”。 下一个访问站点的客户将获得当前值(可以打开或关闭) 相反,您应该仅为该客户切换该值。我为
Configuration
类编写了一个覆盖,检查您是否正在尝试获取PS\u CATALOG\u模式
,然后检查您是否已登录并返回0或1
。请小心使用静态变量缓存此值(因此不必多次检查)
但这种解决方案也有缺陷。它每次都检查请求配置变量的键
更好的解决方案是在会话期间更改此值。在会话期间,配置变量实际上保存在PHP数组中。
您应该在此处进行更改:
可能通过覆盖
这就是我通过覆盖加载配置
想到的:
<?php
// placed in /override/classes/Configuration.php
class Configuration extends ConfigurationCore
{
public static function loadConfiguration()
{
parent::loadConfiguration();
// 'global' because I assume you're not runing multishop
self::$_cache[self::$definition['table']][0]['global']['PS_CATALOG_MODE'] = !Context::getContext()->customer->isLogged();
}
}
为什么不直接使用组设置呢?客户组设置允许您为访客将“显示价格”选项设置为“false”,为客户设置为“true”。我们使用gskema找到的解决方案是覆盖配置类的get()函数:
<?php
// placed in /override/classes/Configuration.php
class Configuration extends ConfigurationCore
{
public static function get($key, $id_lang = null, $id_shop_group = null, $id_shop = null)
{
if (defined('_PS_DO_NOT_LOAD_CONFIGURATION_') && _PS_DO_NOT_LOAD_CONFIGURATION_) {
return false;
}
// If conf if not initialized, try manual query
if (!isset(self::$_cache[self::$definition['table']])) {
Configuration::loadConfiguration();
if (!self::$_cache[self::$definition['table']]) {
return Db::getInstance()->getValue('SELECT `value` FROM `'._DB_PREFIX_.bqSQL(self::$definition['table']).'` WHERE `name` = "'.pSQL($key).'"');
}
}
$id_lang = (int)$id_lang;
if ($id_shop === null || !Shop::isFeatureActive()) {
$id_shop = Shop::getContextShopID(true);
}
if ($id_shop_group === null || !Shop::isFeatureActive()) {
$id_shop_group = Shop::getContextShopGroupID(true);
}
if (!isset(self::$_cache[self::$definition['table']][$id_lang])) {
$id_lang = 0;
}
if ($id_shop && Configuration::hasKey($key, $id_lang, null, $id_shop)) {
if($key == 'PS_CATALOG_MODE' && Context::getContext()->controller->controller_type == 'front')
{
return !Context::getContext()->customer->isLogged() || self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop][$key];
} else {
return self::$_cache[self::$definition['table']][$id_lang]['shop'][$id_shop][$key];
}
} elseif ($id_shop_group && Configuration::hasKey($key, $id_lang, $id_shop_group)) {
if($key == 'PS_CATALOG_MODE' && Context::getContext()->controller->controller_type == 'front')
{
return !Context::getContext()->customer->isLogged() || self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group][$key];
} else {
return self::$_cache[self::$definition['table']][$id_lang]['group'][$id_shop_group][$key];
}
} elseif (Configuration::hasKey($key, $id_lang)) {
if($key == 'PS_CATALOG_MODE' && Context::getContext()->controller->controller_type == 'front')
{
return !Context::getContext()->customer->isLogged() || self::$_cache[self::$definition['table']][$id_lang]['global'][$key];
} else {
return self::$_cache[self::$definition['table']][$id_lang]['global'][$key];
}
}
return false;
}
}
感谢您提供的信息。我明白你说的“不稳定”是什么意思,但这是我找到的最简单的方法。我看到了你刚才提到的函数loadConfiguration,但我很震惊,我自己还不能定制它。顺便说一句,这似乎是解决办法。你能给我更多的信息/提示,以最好的方式覆盖它吗?如果我能学会如何做并提高自己,我会很高兴的。再次感谢。嗨!按照你的建议做,效果很好,但我不确定我做得对。你能检查一下这个代码并告诉我它是好的还是需要改进?如果你能确认这个代码是好的,我会用这个来编辑这个问题来分享解决方案。谢谢@AdrienLeber我用LoadConfiguration
overrideWaw更新了我的答案,它确实比我用get()函数所做的要好。谢谢,我学到了一些有用的东西。祝你星期天愉快@阿德里安·勒伯,谢谢!它有用吗?我也忘了在数值上加上否定,谢谢你的回答,但自从上次回复后,我明白我得到了用另一种方式写模块的机会。谢谢你的提示!