如何解决在主页中获取客户 ID - Magento 2
我需要在主页中获取主要客户 ID,但没有会话接口来提供此信息。
我试过论文:
\Magento\Backend\Model\Session
\Magento\Catalog\Model\Session
\Magento\Checkout\Model\Session
\Magento\Customer\Model\Session
\Magento\Newsletter\Model\Session
是否存在一个界面来在每一页上提供客户 ID?
解决方法
magento2 中提供了获取客户 ID 的方法,其中一种方法是使用对象管理器,如下代码所示。
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
$customerId = $customerSession->getCustomer()->getId(); // get the customer Id
,
命名空间供应商\模块\块;
class Form extends \Magento\Framework\View\Element\Template
protected $customerSession;
/**
* Construct
*
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,\Magento\Customer\Model\Session $customerSession,array $data = []
) {
parent::__construct($context,$data);
$this->customerSession = $customerSession;
}
public function _prepareLayout()
{
var_dump($this->customerSession->getCustomer()->getId());
exit();
return parent::_prepareLayout();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。