2015-06-13 94 views
1

我正在與WC第一次合作,我正在追逐我的尾巴。在「訂單」頁面中顯示商品圖像 - Woocommerce

在「訂單」頁面中,顧客可以看到他們所做的所有購買,該數組顯示了有關訂單的一些基本信息。 我還需要顯示客戶購買的產品的圖像。

訂單似乎是自定義文章,但我如何獲得圖像產品?

我意識到這個問題太模糊。任何指針都會有很大的幫助。

回答

5

訂單是shop_order類型的自定義文章類型。訂單本身並沒有縮略圖,但是訂單有購買的產品列表,每個產品都有縮略圖的可能性。

可以在order-details.php模板看看如何將所有與任何訂單對象相關的項目/產品... $order->get_items()

這將返回存儲在單獨的數據庫表中的數據的數組。使用$item變量,您可以獲得原始產品,並且您可以在鏈接的模板中看到正在發送到order-details-item.php$product變量定義爲order->get_product_from_item($item)

無論如何,一旦你有$product對象,你可以使用$product->get_image()來檢索產品的圖像。

作爲一個簡單的例子,這將顯示所有以購買產品的縮略圖999

$order_id = 999; 
$order = wc_get_order($order_id); 
foreach($order->get_items() as $item_id => $item) { 
     $product = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item); 
     echo $product->get_image(); 
} 

嵌套這裏面你的循環中:

foreach ($customer_orders as $customer_order) { 
    $order = wc_get_order(); 
    $order->populate($customer_order); 
    foreach($order->get_items() as $item_id => $item) { 
      $product' = apply_filters('woocommerce_order_item_product', $order->get_product_from_item($item), $item); 
      echo $product->get_image(); 
    } 
} 

雖然通常情況下,order-details.php模板應該鏈接到每個訂單的概述。

+0

謝謝@helgatheviking!我能再問你一件事嗎?在我使用的主題中,我有這個數組foreach($ customer_orders as $ customer_order){ \t $ order = wc_get_order(); \t $ order-> populate($ customer_order); \t $ item_count = $ order-> get_item_count();' 如何將它與您的代碼結合? –

+0

你可以'嵌套'foreach循環。 – helgatheviking

+0

是的,我明白了。雖然你是救世主!一個注意事項:在你的代碼中你寫'$ product'='我是PHP的新手,但是你有一個報價,它會返回一個錯誤。只要報價被刪除,錯誤也會被刪除。 –

相關問題