簡體   English   中英

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

但這會返回錯誤。

發布此問題后不久,我就知道了。

只需更改此行

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM