0

How to get Magento 2 customer group ID if user is logged in, in a .phtml file? I have already tried with:

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 doesn't work, I get this error:

Parse error: syntax error, unexpected 'protected' (T_PROTECTED), expecting end of file in..
2
1

If you are using the above code directly in your phtml file, Then it does not work.

To get logged in customer group id directly in phtml without using Dependency Injection, You can use object manager to get group id in your phtml file.

<?php
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $customerSession = $objectManager->create('Magento\Customer\Model\Session');
  if ($customerSession->isLoggedIn()) {
      echo 'Customer Group Id: ' .  $customerSession->getCustomer()->getGroupId();
  }
?>

Using Dependency Injection

app/code/Vendor/Module/Block/CustomBlock.php

<?php
namespace Vendor\Module\Block;
class CustomBlock extends \Magento\Framework\View\Element\Template
{
    protected $_customerSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\SessionFactory $customerSession,
        array $data = []
    ) {
        $this->_customerSession = $customerSession->create();
        parent::__construct($context, $data);
    }

    public function getCustomerGroupId() {
        if ($this->_customerSession->isLoggedIn()) {
            return $this->_customerSession->getCustomerData()->getGroupId();
        }
        return false;
    }
}

Now, you can use the functions in your phtml file as follows.

<?php echo 'Customer Group Id: ' . $block->getCustomerGroupId();  ?>

Hope this will help you.

1
  • That works great, thank you!
    – alexcr
    Apr 8 '20 at 4:46
1

As you want Customer group id in phtml Then use HTTP context Variable and View model.. using this two you can get Customer group id for current logged in Customer. Also, assume that You have using 2.2.X or 2.3.X version.

First, a take look On HTTP context Variable https://magento.stackexchange.com/a/179253/4564

Create a View model Class to your module atapp/code/{VendorName}/{ModuleName}/ViewModel/CurrentCustomerGroup.php.

And here the code of this class

<?php 
namespace {VendorName}\{ModuleName}\ViewModel;

use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Customer\Model\Context as CustomerContext;

class CurrentCustomerGroup implements ArgumentInterface
{
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    private  $httpContext;

    public function __construct(
        \Magento\Framework\App\Http\Context $httpContext

    ) {
        $this->httpContext = $httpContext;

    }
    public function getCustomerGroupCode()
    {
        if($this->httpContext->getValue(CustomerContext::CONTEXT_AUTH)){
            return $this->httpContext->getValue(CustomerContext::CONTEXT_GROUP);
        }

        return false;
    }
}

After that Class to your block class using Layout

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="{BlockNameinLayourFile}">
            <arguments>
                <argument name="view_model" xsi:type="object">{VendorName}\{ModuleName}\ViewModel\CurrentCustomerGroup</argument>
            </arguments>
        </referenceBlock>
    </body>
</page> 

Now, you can access getCustomerGroupCode() in your phtml using

<?php
$viewModel = $block->getViewModel();
?>
<?= $block->escapeHtml($viewModel->getCustomerGroupCode()); ?>
1
0
protected $_customerSession;

protected $customerEntityFactory;

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

public function getGroupId(){
    if($this->_customerSession->isLoggedIn()):
        $customerId = $this->_customerSession->getCustomerId();
        $customer = $this->customerEntityFactory->create()->load($customerId);
        echo $customerGroup = $customer->getGroupId();
    endif;
}
0

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

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.