M2 全页面缓存下获取customer group id
M2 全页面缓存下获取customer group id
在线上开启全页面缓存后,导致根据Customer session获取GroupId会失效。
应该这样来获取
<?php use MagentoCustomerModelContext; class Data extends MagentoFrameworkAppHelperAbstractHelper { public function getCustomerGroup() { $currentGroupId = 0; $objectManager = MagentoFrameworkAppObjectManager::getInstance(); $httpContext = $objectManager->get('MagentoFrameworkAppHttpContext'); if($httpContext->getValue(Context::CONTEXT_AUTH)){ $currentGroupId = $httpContext->getValue(Context::CONTEXT_GROUP); } if(!$currentGroupId){ $customerSession = $objectManager->get('MagentoCustomerModelSession'); if ($customerSession->isLoggedIn()) { $currentGroupId = $customerSession->getCustomer()->getGroupId(); } } $customerGroup = $objectManager->get('MagentoCustomerModelGroup'); $group = $customerGroup->load($currentGroupId); return $group; } } ?>
也就是说在
MagentoCustomerModelAppActionContextPlugin
类里。m2默认就已经给我们准备了group id。
因为他们也发现了这个问题,以后扩展的话也方便,直接重写这个ContextPlugin就行了。