CS-Cart多商家:按商家配置未登录用户隐藏价格及加购按钮
实现CS-Cart多商家按商家单独控制未登录用户的购物权限
要实现按商家单独配置未登录用户能否查看商品价格、使用「add to cart」按钮,需要通过商家自定义字段+模板/后端逻辑修改来实现,具体步骤如下:
1. 给商家添加权限控制自定义字段
- 进入后台「商家管理 > 商家配置」,点击「添加字段」
- 字段类型选择「布尔值(是/否)」,字段名设为
restrict_unlogged_shopping,显示标签设为「限制未登录用户查看价格和加购」 - 保存后,每个商家的编辑页面会新增这个开关,商家可自行开启/关闭该限制
2. 修改前端模板,控制价格和加购按钮显示
找到产品列表页和详情页的模板文件,分别修改显示逻辑:
产品列表页(skins/[你的皮肤]/customer/products/products_list.tpl)
在价格和加购按钮的代码块外添加判断:
{if !$auth.user_id && $product.company_data.restrict_unlogged_shopping == "Y"} <span class="price-hidden">请登录查看价格</span> <button class="btn btn-default disabled" disabled>请登录后加购</button> {else} <!-- 原有价格显示代码 --> {include file="common/price.tpl" product=$product} <!-- 原有加购按钮代码 --> {include file="buttons/add_to_cart.tpl" product=$product} {/if}
产品详情页(skins/[你的皮肤]/customer/products/product_details.tpl)
同样在价格区域和加购按钮区域添加判断:
{if !$auth.user_id && $product.company_data.restrict_unlogged_shopping == "Y"} <div class="product-price"> <p class="text-muted">请登录后查看价格</p> </div> <div class="product-add-to-cart"> <button class="btn btn-primary disabled" disabled>请登录后加入购物车</button> </div> {else} <!-- 原有价格展示代码 --> {include file="common/price.tpl" product=$product} <!-- 原有加购按钮代码 --> {include file="buttons/add_to_cart.tpl" product=$product} {/if}
3. 后端添加购物车验证,防止未登录用户绕过前端限制
修改app/functions/fn.cart.php中的fn_add_product_to_cart函数,在函数开头添加验证逻辑:
// 验证未登录用户是否被商家限制加购 if (!$auth['user_id']) { $product_id = $product_data['product_id'] ?? key($product_data); $product = fn_get_product_data($product_id, $auth, CART_LANGUAGE, '', true, true, true, true, false, false, true); if (!empty($product['company_data']['restrict_unlogged_shopping']) && $product['company_data']['restrict_unlogged_shopping'] == 'Y') { fn_set_notification('E', __('error'), __('please_login_to_add_product')); return false; } }
然后在app/languages/[你的语言]/common.php中添加对应的提示语:
'please_login_to_add_product' => '请登录后才能添加该商品到购物车',
4. 优先级说明
商家的自定义开关优先级高于全局的「Allow shopping for unlogged customers」设置:
- 如果商家开启了限制,无论全局设置是否允许未登录购物,该商家的商品都会对未登录用户隐藏价格、禁用加购按钮
- 如果商家未开启限制,则遵循全局设置的规则
内容的提问来源于stack exchange,提问作者Mukesh Mali
相关产品推荐
相关产品推荐

