To get logged in customer group id directly in phtml without using Dependency Injection, You can use the 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 $customer= $block->getLayout()->createBlock(‘JJ\LoggedOut\Block\CustomerGroupId’); $group = $customer->getCustomerGroupId();?>