从id - magento2获取客户数据

问题描述 投票:2回答:9

我们如何才能从ID获取客户数据而不是来自Magento2中的客户会话。

请告诉我。

magento2 magento-2.0
9个回答
3
投票

以下是在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('[email protected]');// load customer by email address
    //echo $customer->getEntityId();
    $customer->load('1');// load customer by using ID
    $data= $customer->getData();
    print_r($data);

3
投票

您可以在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获取客户帐单地址和送货地址。完整的帖子在这个link

出于演示目的,我使用了Objectmanager。应始终使用构造函数方法。


3
投票

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

}

?>

2
投票

您可以通过以下方式通过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
    }

}

1
投票

建议使用依赖注入而不是使用对象管理器。

像块一样创建块

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(); ?>

0
投票
 $customer =  $this->objectManager->create('Magento\Customer\Model\Customer')->load(1);

您应该使用依赖注入而不是objectManager。


0
投票

这是旧的和Magento更新此您可以从以下方法获取客户数据也有更多选项可用

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load(1);

您还可以使用Magento \ Customer \ Model \ Customer来使用依赖注入。使用对象管理器的主要好处是它可以在phtml中使用。


0
投票

我在缓存启用时遇到同样的问题我无法获得客户会话。但我找到以下解决方案

    /**
     * @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());
    }

0
投票
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);

    $xxx = $customer->getData('xxx'); 
© www.soinside.com 2019 - 2024. All rights reserved.