Magento 2 通过电子邮件从订单中获取客户数据
Magento 2 Get Customer Data from Order by Email
我已经为事件创建了一个观察者: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());
但是那个returns一个错误。
发布这个问题后不久,我就弄明白了。
只需更改此行
$customer = $this->_customerRepositoryInterface->getById($order->getCustomerId());
到
$customer = $this->_customerRepositoryInterface->get($order->getCustomerEmail(), $websiteId = null);
我已经为事件创建了一个观察者: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());
但是那个returns一个错误。
发布这个问题后不久,我就弄明白了。
只需更改此行
$customer = $this->_customerRepositoryInterface->getById($order->getCustomerId());
到
$customer = $this->_customerRepositoryInterface->get($order->getCustomerEmail(), $websiteId = null);