13

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?

14

Magento\Customer\Model\Session $customerSession using this class you will get 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 customer logged in

7

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;

}
  • But it is return 1 (Id of General Customer Group)when I am not logged in. – Rohan Hapani May 2 '17 at 11:43
  • 1
    @RohanHapani added the code kindly check and feedback.. – Qaisar Satti May 2 '17 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? – Qaisar Satti May 2 '17 at 11:49
  • Yeah...Now It's working... Thank You Sir :) – Rohan Hapani May 2 '17 at 11:52
5

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://ranasohel.me/2017/05/05/how-to-get-customer-id-from-block-when-full-page-cache-enable-in-magento-2/

2

Try this to get current customer group Id and name for both logged and not logged in customer

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;
}

This May be useful for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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