从id - magento2获取客户数据

时间:2016-02-24 20:22:15

标签: magento2 magento-2.0

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

请告诉我。

9 个答案:

答案 0 :(得分: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('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);

答案 1 :(得分:3)

使用api工厂加载客户。这是正确的方法。

android:configChanges="orientation|screenSize"

答案 2 :(得分: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
    }

}

答案 3 :(得分: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(); ?>

答案 4 :(得分:1)

您可以在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。一个应该始终使用构造函数方法。

答案 5 :(得分:0)

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

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

答案 6 :(得分:0)

这是旧的Ans Magento更新了这个 您可以从以下方法获取客户数据,也可以使用更多选项

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

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

答案 7 :(得分: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());
    }

答案 8 :(得分:0)

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

    $xxx = $customer->getData('xxx');