如何在magento2中获取当前客户组ID


Answers:


19

Magento\Customer\Model\Session $customerSession 使用此类,您将获得当前的客户组ID

protected $_customerSession;

public function __construct(
        \Magento\Customer\Model\Session $customerSession,
    ) {
        $this->_customerSession = $customerSession;
    }

public function getGroupId(){
 if($this->_customerSession->isLoggedIn()):
        echo $customerGroup=$this->_customerSession->getCustomer()->getGroupId();
    endif;
}

注意:只有在客户登录后,您才能获得客户ID


7

您可以通过以下代码获取组ID

protected $_customerSession;

public function __construct(
        ....    
        \Magento\Customer\Model\Session $customerSession,
        ....
    ) {


        $this->_customerSession = $customerSession;

    }

public function getGroupId(){
 if($this->_customerSession->isLoggedIn()):
        echo $customerGroup=$this->_customerSession->getCustomer()->getGroupId();
    endif;

}

但它是返回1(一般客户群的标识)时,我没有登录。
罗汉Hapani

1
@RohanHapani添加的代码亲切检查和反馈..
凯萨尔萨蒂

1
@RohanHapani我测试了此代码,未登录用户未显示groupid您if($this->_customerSession->isLoggedIn()):是否已登录 isLoggedIn检查?
卡萨尔·萨蒂

是的...现在工作了...谢谢您,先生:)
Rohan Hapani

6

默认情况下,Magento将清除客户会话:\Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml

/magento//a/92133/33057

看一看:

供应商/ magento /模块客户/模型/Context.php

/**
 * Customer group cache context
 */
const CONTEXT_GROUP = 'customer_group';
/**
 * Customer authorization cache context
 */
const CONTEXT_AUTH = 'customer_logged_in';

我们可以检查已登录的客户和客户组:

 /**
 * @var \Magento\Framework\App\Http\Context $httpContext
 */
$isLogged = $this->httpContext->getValue(Context::CONTEXT_AUTH);
$customerGroupId = $this->httpContext->getValue(Context::CONTEXT_GROUP);

将这些代码行放在您的块中。

这里还有另一个很好的解释:

https://ranasohel.me/2017/05/05/how-to-get-customer-id-from-block-when-full-page-cache-enable-in-magento-2/


2

尝试此操作以获取已登录和未登录客户的当前客户组ID和名称

protected $_customerSession;

protected $_customerGroupCollection;

public function __construct(
    ....    
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Customer\Model\Group $customerGroupCollection,
    ....
) {


    $this->_customerSession = $customerSession;
    $this->_customerGroupCollection = $customerGroupCollection;

}

public function getCustomerGroup()
{
        echo $currentGroupId = $this->_customerSession->getCustomer()->getGroupId(); //Get current customer group ID
        $collection = $this->_customerGroupCollection->load($currentGroupId); 
        echo $collection->getCustomerGroupCode();//Get current customer group name
}

1
protected $_customerSession;

public function __construct(
        \Magento\Customer\Model\Session $customerSession,
    ) {
        $this->_customerSession = $customerSession;
    }

public function getGroupId(){
 if($this->_customerSession->isLoggedIn()):
        echo $customerGroup=$this->_customerSession->getCustomer()->getGroupId();
    endif;
}

这可能对您有用。


0

如果使用缓存,则使用\ Magento \ Customer \ Model \ Session可能会失败。

您最好使用:

private $sessionProxy;

public function __construct(
    use Magento\Customer\Model\Session\Proxy $sessionProxy,
) {
    $this->sessionProxy= $sessionProxy;
}

// may return groupId or \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID  
public function getGroupId(){
   $this->sessionProxy->getCustomer()->getGroupId();
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.