我们如何从 id 而不是从 Magento2 的客户会话中获取客户数据。
请告诉我。
您可以从 Magento 2 中的客户 ID 获取所有客户数据。这是代码片段
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')-
>create();
$customerId = 1;
$customer = $customerFactory->load($customerId);
//fetch whole customer information
echo "<pre>";
print_r($customer->getData());
//fetch specific information
echo $customer->getEmail();
echo $customer->getFirstName();
echo $customer->getLastName();
您还可以从客户 ID 中获取客户帐单地址和送货地址。完整的帖子在这个链接中
为了演示的目的,我使用了 Objectmanager。应该始终使用构造函数方法。
使用 api 工厂加载客户。这是正确的方法。
<?php
namespace Yourcompany\Customer\Helper {
/**
* Eav data helper
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $customerRepository;
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterfaceFactory $customerRepositoryFactory) {
$this->customerRepository = $customerRepositoryFactory->create();
}
public function LoadCustomerById($customerId) {
$cst = $this->customerRepository->getById($customerId);
return $cst;
}
}
?>
这是在magento 2版本中以编程方式使用id获取客户数据的代码片段
use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$websiteId = $storeManager->getWebsite()->getWebsiteId();
// Get Store ID
$store = $storeManager->getStore();
$storeId = $store->getStoreId();
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
$customer=$customerFactory->create();
$customer->setWebsiteId($websiteId);
//$customer->loadByEmail('example@gmail.com');// load customer by email address
//echo $customer->getEntityId();
$customer->load('1');// load customer by using ID
$data= $customer->getData();
print_r($data);
您可以通过以下方式通过id获取客户数据
namespace Vendor\Module\Block\Index;
class Index extends \Magento\Framework\View\Element\Template
{
protected $_customer;
public function __construct(
\Magento\Customer\Model\Customer $customer,
\Magento\Backend\Block\Template\Context $context
)
{
$this->_customer = $customer;
parent::__construct($context);
}
public function getCustomer()
{
$customerId = '3'; //You customer ID
$customer = $this->_customer->getCollection()->addAttributeToFilter('entity_id', array('eq' => '3'));
print_r($customer->getData());//Customer data by customer ID
}
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);
$xxx = $customer->getData('xxx');
建议使用依赖注入而不是使用对象管理器。
像这样创建块
namespace Lapisbard\General\Block;
use Magento\Customer\Model\Session;
class CustomerAccount extends \Magento\Framework\View\Element\Template {
public function __construct(
Session $customerSession,
\Magento\Framework\View\Element\Template\Context $context
)
{
parent::__construct($context);
$this->_customerSession = $customerSession;
}
public function getCustomerName(){
$this->_customerSession->getCustomer()->getName();
}
}
并在您的模板中使用它
<?php echo $block->getCustomerName(); ?>
这是旧的 Ans Magento 已更新您可以通过以下方法获取客户数据,也可以使用更多选项
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load(1);
您也可以使用 Magento\Customer\Model\Customer 使用依赖注入的块。使用对象管理器的主要好处是它可以在 phtml 中使用。
$customer = $this->objectManager->create('Magento\Customer\Model\Customer')->load(1);
Instead of objectManager you should use dependency injection.
启用缓存时我面临同样的问题我无法获得客户会话。但我找到以下解决方案
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
public function __construct(Template\Context $context,
\Magento\Framework\App\Request\Http $request,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Customer\Model\SessionFactory $customerSession
)
{
$this->request = $request;
$this->customerRepository = $customerRepository;
$this->_customerSession = $customerSession;
parent::__construct($context);
}
public function getCustomerId(){
$customer = $this->_customerSession->create();
var_dump($customer->getCustomer()->getId());
}
在块中使用上面的代码即使启用了缓存也可以正常工作。
第二种解决方案:
在您的 xml 中添加cacheable="false"
<referenceContainer name="content">
<block class="Vendor\Modulename\Block\Customer" name="customer.session.data" template="Vendor_Modulename::customertab.phtml" cacheable="false" />
</referenceContainer>
在块中添加以下代码:
/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSession;
/**
* Construct
*
* @param \Magento\Framework\View\Element\Template\Context $context
*/
public function __construct(Template\Context $context,
\Magento\Framework\App\Request\Http $request,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Customer\Model\Session $customerSession
)
{
$this->request = $request;
$this->customerRepository = $customerRepository;
$this->_customerSession = $customerSession;
parent::__construct($context);
}
public function getOrderData(){
$customerId = $this->_customerSession->getCustomerId();
var_dump($this->_customerSession->getCustomer());
}
通过 ID 在 Magento2 中获取客户详细信息
use Magento\Customer\Model\CustomerFactory;
class CustomClass {
/**
* @var CustomerFactory
*/
private $customerFactory;
public function __construct(
CustomerFactory $customerFactory,
) {
$this->customerFactory = $customerFactory;
}
public function getCustomerDetails() {
$customerId=6;
// $customerId =$this->getCustomerIdByEmail();
// $customerId =$this->getCustomerIdByMobile();
$loadData =$this->customerFactory->create()->load($customerId);
$allData = $loadData->getData());
print_r($allData);
}
}
通过电子邮件获取客户 ID
public function getCustomerIdByEmail() {
$customerId = $this->customerFactory->create()->getCollection()
->addFieldToFilter('email', $email)
->getFirstItem()->getId();
}
通过手机号码获取客户ID
public function getCustomerIdByMobile() {
$customerId = $this->customerFactory->create()->getCollection()
->addFieldToFilter('mobile_number', $mobile_number)
->getFirstItem()->getId();
}