【问题标题】:Magento 2 Get Customer Data from Order by EmailMagento 2 通过电子邮件从订单中获取客户数据
【发布时间】:2018-03-05 21:20:51
【问题描述】:

我已经为事件创建了一个观察者:sales_order_place_after

我能够从customerId获取客户数据

<?php
namespace MyCompany\MyModule\Observer\Sales\Order;

use Magento\Framework\Event\ObserverInterface;

class Salesrep implements ObserverInterface
{
  public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
  ){
    $this->_customerRepositoryInterface = $customerRepositoryInterface;
  }

  public function execute(\Magento\Framework\Event\Observer $observer)
   {
     $order = $observer->getEvent()->getOrder();
     $customer = $this->_customerRepositoryInterface->getById($order->getCustomerId());

     $salesrep = $customer->getCustomAttribute('salesrep')->getValue();
     $order->setSalesrep($salesrep);
   }
}

如果客户以客人身份订购,我如何通过电子邮件获取客户详细信息?

我也试过了

$customer = $this-&gt;_customerRepositoryInterface-&gt;getByEmail($order-&gt;getCustomerEmail());

但这会返回错误。

【问题讨论】:

    标签: php magento2 magento2.2


    【解决方案1】:

    发布此问题后不久,我就可以弄清楚了。

    只需要改变这一行

    $customer = $this-&gt;_customerRepositoryInterface-&gt;getById($order-&gt;getCustomerId());

    $customer = $this-&gt;_customerRepositoryInterface-&gt;get($order-&gt;getCustomerEmail(), $websiteId = null);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-01
      • 2015-06-13
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      相关资源
      最近更新 更多