Frage

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..
War es hilfreich?

Lösung

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.

Andere Tipps

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

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();
   }
 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top