0

我为该事件创建了一个观察者: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->_customerRepositoryInterface->getByEmail($order->getCustomerEmail());

但这会返回错误。

4

1 回答 1

1

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

只需要更改此行

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

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

于 2018-03-05T22:11:32.960 回答