Làm cách nào để có id nhóm khách hàng hiện tại trong magento2


15

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:


18

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


7

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;

}

Nhưng đó là trả lại 1 (Id của Nhóm khách hàng chung) khi tôi chưa đăng nhập.
— Rohan Hapani

1
@RohanHapani đã thêm mã vui lòng kiểm tra và phản hồi ..
— Qaisar Satti

1
@RohanHapani tôi đã kiểm tra mã này, nó không hiển thị groupid vì không đăng nhập người dùng bạn đã đăng nhập chưa if($this->_customerSession->isLoggedIn()):?
— Qaisar Satti

Vâng ... Bây giờ nó đang hoạt động ... Cảm ơn ngài Sir :)
— Rohan Hapani

6

Theo mặc định, Magento sẽ xóa phiên khách hàng : \Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXml.

/magento//a/92133/33057

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:

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


2

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
}

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

Điều này có thể hữu ích cho bạn.


0

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();
}
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.