2013-05-11 36 views
0

我正在爲我的Magento 1.7安裝構建一個小模塊。從sales_order_invoice_pay事件中檢索原始客戶對象

當觸發sales_order_payment_pay事件時,我的函數被調用。

基本上,它會查看客戶的訂單以檢查是否存在特定的魔術產品。

// Retrieve the order 
    $order = $observer->getPayment()->getOrder(); 
    // Look for product ID 999, whatever that is 

    try { 
     $magic_product = $order->getItemById(999); 


     if ($magic_product){ 
      // The order contains our magic product; let's find our customer object and do something with it. 
      //**$customer = $order->getCustomer();** 
      $customer->setData("foo", "bar"); 
      $customer->save(); 
     } 
    } catch (Exception $e) { 
     // Fail silently, I don't care... 
    } 

我想在這裏找到一種方法來上升對象層次結構來檢索訂單的客戶(如果有的話)。

使用getCustomerName()我可以得到它的名字,但我找不到任何方式來找到對象本身,而不訴諸某種破解。我不喜歡黑客。任何人都可以幫助我?

+0

吉茲人,如果你投下來,發表評論! – 2013-05-11 02:11:18

+0

沒有理由downvote。這種情況是特殊的,因爲你不能使用通常的Mage :: xxx(客戶/會話)方法,因爲異步調用(從後端,從PayPal IPN請求等)。 – 2015-08-19 23:33:45

回答

3

如果你想獲取有關訂單的客戶對象,你必須加載和訂單都有客戶ID:

$customerId = $order->getCustomerId(); 
$customer = Mage::getModel('customer/customer')->load($customerId); 
// customer stuff 
+0

就是這樣!經過幾個小時的工作,我完全無法看到現在看起來很明顯的東西。謝謝! – 2013-05-11 23:36:07

1
$event = $observer->getEvent(); 
$invoice = $event->getInvoice(); 
$order = $invoice->getOrder(); 
echo $order->getCustomerId(); 

嘗試的情況下,使用這個你得到一個空的客戶ID