回答:
Magento\Customer\Model\Session $customerSession このクラスを使用すると、現在の顧客グループ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;
}注:顧客がログインした場合にのみ顧客IDを取得します
次のコードでグループ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;
}if($this->_customerSession->isLoggedIn()):グループID は表示されていませ   んか?
                    デフォルトでは、Magentoは顧客セッションをクリアします:\Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml。
見てみましょう:
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';ログインしている顧客と顧客グループを確認できます。
 /**
 * @var \Magento\Framework\App\Http\Context $httpContext
 */
$isLogged = $this->httpContext->getValue(Context::CONTEXT_AUTH);
$customerGroupId = $this->httpContext->getValue(Context::CONTEXT_GROUP);これらのコード行をブロックに入れます。
ここに別の良い説明があります:
これを試して、ログインしている顧客とログインしていない顧客の両方の現在の顧客グループIDと名前を取得します
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
}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;
}これは役に立つかもしれません。
キャッシングを使用すると、\ Magento \ Customer \ Model \ Sessionの使用が失敗する場合があります。
より良い使用する必要があります:
private $sessionProxy;
public function __construct(
    use 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();
}