magepow / customersession
帮助您在启用varnish缓存时获取客户会话数据。
1.0.0
2021-07-21 08:54 UTC
Requires
- magepow/core: ^1.0.0
README
Magento 2 CustomerSession 扩展免费版
在 Magento 2 中,当启用 varnish 缓存时,我们无法从 \Magento\Customer\Model\Session 获取客户会话数据,所以我们使用 \Magento\Framework\App\Http\Context。但是默认情况下,\Magento\Framework\App\Http\Context 只提供客户组(customer_group)和客户登录状态(customer_logged_in)的值。由 Magepow 提供的 CustomerSession 扩展免费版 允许您获取不仅仅是客户组(customer_group)和客户登录状态(customer_logged_in),还包括客户 ID、名称、电子邮件和其他客户属性(如果已设置)。
如何安装 Magento 2 CustomerSession 扩展免费版
✓ 通过 composer 安装 Magepow CustomerSession(推荐)
在 Magento 2 根目录下运行以下命令
composer require magepow/customersession
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
Magepow CustomerSession 用户指南
示例
1. 获取 customer_logged_in
$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
1. 获取 customer_group
$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_GROUP);
1. 获取 customer id
$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue('customer_id');
1. 获取 customer name
$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue('customer_name');
1. 获取 customer email
$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue('customer_email');
要获取更多客户属性,您需要编辑 \Magepow\CustomerSession\Plugin\CustomerSessionContext.php 中的 aroundDispatch 方法。
捐赠
如果这个项目帮助您节省了开发时间,您可以给我一杯咖啡 :)
