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?

Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
Rohan Hapani
  • 17,388
  • 9
  • 54
  • 96

8 Answers8

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

Bhavin Gohil
  • 501
  • 3
  • 9
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/

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
  • 1
    Here is the new url: https://sohel.dev/how-to-get-customer-id-from-block-when-full-page-cache-enable-in-magento-2/ – Sohel Rana Apr 08 '20 at 05:12
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;

}
Qaisar Satti
  • 32,469
  • 18
  • 85
  • 137
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 }

Bhavin Gohil
  • 501
  • 3
  • 9
Prince Patel
  • 22,708
  • 10
  • 97
  • 119
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(); }

Antoine Martin
  • 582
  • 4
  • 17
Yann Charlou
  • 71
  • 1
  • 1
2

You can also get it from the session

$group_id=$_SESSION['customer_base']['customer_group_id'];
Yasir Minhaj
  • 107
  • 8
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();
Raul Sanchez
  • 3,036
  • 3
  • 28
  • 63
  • It will not work in template. Needs to extend \Magento\Framework\App\Helper\AbstractHelper in order to use in template. – Zan Kolev May 09 '22 at 09:49