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