22

I want to get current customer group id in phtml file. When I am not logged in still it is return general type customer group. How can get proper output?

8 Answers 8

29

Magento\Customer\Model\Session $customerSession using this class you will get the current customer group 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;
}

NOTE: You only get customer id if the customer logged in

11

By default, Magento will clear the customer session: \Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml.

https://magento.stackexchange.com/a/92133/33057

Take a look:

vendor/magento/module-customer/Model/Context.php

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

We can check the logged in customer and customer group:

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

Put these code lines in your block.

There is another good explanation here:

https://sohel.dev/how-to-get-customer-id-from-block-when-full-page-cache-enable-in-magento-2/

1
8

you can get group Id by following code

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;

}
3
  • But it is return 1 (Id of General Customer Group)when I am not logged in. Commented May 2, 2017 at 11:43
  • 1
    @RohanHapani added the code kindly check and feedback.. Commented May 2, 2017 at 11:46
  • 1
    @RohanHapani i tested this code it is not showing groupid for not log in user did you if($this->_customerSession->isLoggedIn()): isLoggedIn check? Commented May 2, 2017 at 11:49
3

Try this to get the current customer group Id and name for both logged and not logged-in customers.

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
}
0
2

Using \Magento\Customer\Model\Session may fail if you use caching.

You should better use :

private $sessionProxy;
       
public function __construct(
    \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();
}
2

You can also get it from the session

$group_id=$_SESSION['customer_base']['customer_group_id'];
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;
}

This May be useful for you.

0

I believe the easiest way to get this from any template file, as stated in question, would be using this helper https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Customer/Helper/Session/CurrentCustomer.php

Method getCustomer() will run getDepersonalizedCustomer() and set customer group ID in customer object, even in a cached page context

/**
 * Returns current customer according to session and context
 *
 * @return \Magento\Customer\Api\Data\CustomerInterface
 */
public function getCustomer()
{
    if ($this->moduleManager->isEnabled('Magento_PageCache')
        && !$this->request->isAjax()
        && $this->view->isLayoutLoaded()
        && $this->layout->isCacheable()
    ) {
        return $this->getDepersonalizedCustomer();
    } else {
        return $this->getCustomerFromService();
    }
}

/**
 * Returns customer Data with customer group only
 *
 * @return \Magento\Customer\Api\Data\CustomerInterface
 */
protected function getDepersonalizedCustomer()
{
    $customer = $this->customerFactory->create();
    $customer->setGroupId($this->customerSession->getCustomerGroupId());
    return $customer;
}

So, this should work in a template

$customerGroupId = $this->helper('\Magento\Customer\Helper\Session\CurrentCustomer')->getCustomer()->getGroupId();
1
  • It will not work in template. Needs to extend \Magento\Framework\App\Helper\AbstractHelper in order to use in template.
    – Zan Kolev
    Commented May 9, 2022 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.