2017-07-11 50 views
1

我試圖修復unsupported SagePay plugin中的一些棄用函數。

如何替換WooCommerce中的以下代碼片段?

foreach ($order->get_items() as $item) { 
if ($item['qty']) { 
    $product = $order->get_product_from_item($item); 

我可以看到,這是它的替代:

@Deprecated添加棄用通知在未來的版本。替換$item->get_product()

但只是將其更改爲$product = $item->get_product();不起作用。我也試圖改變該行:

$product = wc_get_product($item['id']);

但在結帳時會導致內部服務器錯誤。

回答

1

您可以在WC_OrderWC_Order_Items對象這樣使用WC_data get_data()方法:

// For testing only 
$order = wc_get_order(500); 

// Iterate though each order item 
foreach ($order->get_items() as $item_id => $item) { 

    // Get the WC_Order_Item_Product object properties in an array 
    $item_data = $item->get_data(); 

    if ($item['quantity'] > 0) { 
     // get the WC_Product object 
     $product = wc_get_product($item['product_id']); 
     // test output 
     print_r($product); 
    } 
} 

或者與WC_Order_Item_Product get_product_id()方法:

// For testing only 
$order = wc_get_order(500); 

// Iterate though each order item 
foreach ($order->get_items() as $item_id => $item) { 
    if($item->get_quantity() > 0){ 
     // Get the product id from WC_Order_Item_Product object 
     $product_id = $item->get_product_id(); 
     // get the WC_Product object 
     $product = wc_get_product($item['product_id']); 

     // test output 
     print_r($product); 
    } 
} 

這一切正在裏面活動主題php文件。

當主題PHP文件測試,我可以$item->get_product();


相關答案得到WC_Product對象:How to get WooCommerce order details

+1

我最初使用'$用品 - > get_product( );'但是因爲PhpStorm強調它是一種不存在的方法,所以我沒有使用它。事實證明,這確實有效。我真的把最後一行改成'$ product = $ item-> get_product();'現在它完美地工作了。 –

+1

@LiamMcArthur我感到非常驚訝,它不工作......所以沒有用新的WC方法更新的愚蠢的編輯...它開心工作。 – LoicTheAztec

相關問題