Jak uzyskać aktualny identyfikator grupy klientów w magento2


16

Chcę uzyskać bieżący identyfikator grupy klientów w pliku phtml . Gdy nie jestem jeszcze zalogowany, jest to grupa klientów w ogólnym typie . Jak uzyskać odpowiednią wydajność?

Odpowiedzi:


19

Magento\Customer\Model\Session $customerSession za pomocą tej klasy otrzymasz aktualny identyfikator grupy klientów

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

UWAGA: Identyfikator klienta otrzymasz tylko wtedy, gdy klient jest zalogowany


7

możesz uzyskać identyfikator grupy, postępując zgodnie z kodem

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;

}

Ale gdy nie jestem zalogowany, zwraca 1 (identyfikator generalnej grupy klientów).
Rohan Hapani

1
@RohanHapani dodał kod uprzejmie sprawdzić i informacje zwrotne ..
Qaisar Satti

1
@RohanHapani Przetestowałem ten kod, nie wyświetla się groupid za niezalogowanie użytkownika. Czy if($this->_customerSession->isLoggedIn()):sprawdziłeś?
Qaisar Satti

Tak ... Teraz działa ... Dziękuję,
proszę

6

Domyślnie, Magento usunie sesję klienta: \Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml.

/magento//a/92133/33057

Spójrz:

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';

Możemy sprawdzić zalogowanego klienta i grupę klientów:

 /**
 * @var \Magento\Framework\App\Http\Context $httpContext
 */
$isLogged = $this->httpContext->getValue(Context::CONTEXT_AUTH);
$customerGroupId = $this->httpContext->getValue(Context::CONTEXT_GROUP);

Umieść te linie kodu w swoim bloku.

Jest tutaj inne dobre wytłumaczenie:

https://ranasohel.me/2017/05/05/how-to-get-customer-id-from-block-when-full-page-cache-enable-in-magento-2/


2

Spróbuj uzyskać bieżący identyfikator grupy klientów i nazwę zarówno dla zalogowanego, jak i niezalogowanego klienta

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
}

1
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;
}

To może być dla Ciebie przydatne.


0

Użycie \ Magento \ Customer \ Model \ Session może się nie powieść, jeśli używasz buforowania.

Lepiej użyj:

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();
}
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.