Tôi muốn nhận id nhóm khách hàng hiện tại trong tệp phtml . Khi tôi không đăng nhập, nó sẽ trả về nhóm khách hàng loại chung . Làm thế nào có thể có được đầu ra thích hợp?
Tôi muốn nhận id nhóm khách hàng hiện tại trong tệp phtml . Khi tôi không đăng nhập, nó sẽ trả về nhóm khách hàng loại chung . Làm thế nào có thể có được đầu ra thích hợp?
Câu trả lời:
Magento\Customer\Model\Session $customerSession sử dụng lớp này bạn sẽ có được id nhóm khách hàng hiện tại
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;
}
LƯU Ý: Bạn chỉ nhận được id khách hàng nếu khách hàng đăng nhập
bạn có thể lấy Id nhóm bằng mã sau
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()):?
                    Theo mặc định, Magento sẽ xóa phiên khách hàng : \Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml.
Hãy xem:
nhà cung cấp / magento / mô-đun khách hàng / Model / Context.php
/**
 * Customer group cache context
 */
const CONTEXT_GROUP = 'customer_group';
/**
 * Customer authorization cache context
 */
const CONTEXT_AUTH = 'customer_logged_in';
Chúng tôi có thể kiểm tra khách hàng đã đăng nhập và nhóm khách hàng:
 /**
 * @var \Magento\Framework\App\Http\Context $httpContext
 */
$isLogged = $this->httpContext->getValue(Context::CONTEXT_AUTH);
$customerGroupId = $this->httpContext->getValue(Context::CONTEXT_GROUP);
Đặt các dòng mã này trong khối của bạn.
Có một lời giải thích tốt khác ở đây:
Hãy thử cách này để nhận Id và tên nhóm khách hàng hiện tại cho cả khách hàng đã đăng nhập và chưa đăng nhập
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;
}
Điều này có thể hữu ích cho bạn.
Sử dụng \ Magento \ Khách hàng \ Model \ Phiên có thể không thành công nếu bạn sử dụng bộ đệm.
Bạn nên sử dụng tốt hơn:
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();
}