ฉันต้องการรับรหัสกลุ่มลูกค้าปัจจุบันในไฟล์phtml เมื่อฉันไม่ได้เข้าก็ยังเป็นผลตอบแทนประเภททั่วไปกลุ่มลูกค้า จะได้รับผลลัพธ์ที่เหมาะสมได้อย่างไร
ฉันต้องการรับรหัสกลุ่มลูกค้าปัจจุบันในไฟล์phtml เมื่อฉันไม่ได้เข้าก็ยังเป็นผลตอบแทนประเภททั่วไปกลุ่มลูกค้า จะได้รับผลลัพธ์ที่เหมาะสมได้อย่างไร
คำตอบ:
Magento\Customer\Model\Session $customerSession เมื่อใช้คลาสนี้คุณจะได้รับรหัสกลุ่มลูกค้าปัจจุบัน
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;
}หมายเหตุ: คุณจะได้รับรหัสลูกค้าเฉพาะเมื่อลูกค้าเข้าสู่ระบบ
คุณสามารถรับรหัสกลุ่มได้โดยใช้รหัสต่อไปนี้
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()):ทำการตรวจสอบแล้วใช่ไหม?
                    \Magento\PageCache\Model\Layout\DepersonalizePlugin::afterGenerateXmlโดยค่าเริ่มต้นวีโอไอพีจะล้างเซสชั่นลูกค้า:
ลองดูสิ:
ผู้ขาย / วีโอไอพี / โมดูลลูกค้า / รุ่น / 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);ใส่รหัสบรรทัดเหล่านี้ในบล็อกของคุณ
มีคำอธิบายที่ดีอีกข้อที่นี่:
ลองใช้วิธีนี้เพื่อรับรหัสกลุ่มลูกค้าปัจจุบันและชื่อทั้งที่บันทึกและไม่ได้ลงชื่อเข้าใช้ลูกค้า
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();
}