簡體   English   中英

在 Woocommerce 3 中獲取退款訂單和退款訂單商品詳情

[英]Get refunded orders and refunded order items details in Woocommerce 3

我看到當我查看訂單時,它會顯示如果整個訂單不是退款的特定項目。

如果商品已退款,是否可以使用WC_Order_Item_Product查找? 另外,有沒有辦法獲得顯示在訂單視圖中項目下方的折扣金額?

我目前正在手動完成,但如果已經有一個功能,我寧願使用它。

要獲得退款訂單,您可以使用一些專用的WC_Order方法,例如以下以 Item Id 作為參數的方法:

$item_qty_refunded = $order->get_qty_refunded_for_item( $item_id ); // Get the refunded amount for a line item.

$item_total_refunded = $order->get_total_refunded_for_item( $item_id ); // Get the refunded amount for a line item.

您可以使用get_refunds()方法訪問此訂單的數組WC_Order_Refund Objects:

因此,您可以使用以下示例代碼:

// Get the WC_Order Object instance (from the order ID)
$order = wc_get_order( $order_id );

// Get the Order refunds (array of refunds)
$order_refunds = $order->get_refunds();

// Loop through the order refunds array
foreach( $order_refunds as $refund ){
    // Loop through the order refund line items
    foreach( $refund->get_items() as $item_id => $item ){

        ## --- Using WC_Order_Item_Product methods --- ##

        $refunded_quantity      = $item->get_quantity(); // Quantity: zero or negative integer
        $refunded_line_subtotal = $item->get_subtotal(); // line subtotal: zero or negative number
        // ... And so on ...

        // Get the original refunded item ID
        $refunded_item_id       = $item->get_meta('_refunded_item_id'); // line subtotal: zero or negative number
    }
}

要獲得管理訂單編輯頁面中顯示的訂單商品折扣值,您將使用以下代碼:

// Get the WC_Order Object instance (from the order ID)
$order = wc_get_order($order_id);

// Loop through the order refund line items
foreach( $order->get_items() as $item_id => $item ){
    $line_subtotal     = $item->get_subtotal();
    $line_total        = $item->get_total();
    $line_subtotal_tax = $item->get_subtotal_tax();
    $line_total_tax    = $item->get_total_tax();
    
    // Get the negative discount values
    $line_discount     = $line_total - $line_subtotal; // (Negative number)
    $line_discount_tax = $line_total_tax - $line_subtotal_tax; // (Negative number)
}

相關答案:

如果您使用get_qty_refunded_for_item( $item_id )get_total_refunded_for_item( $item_id )返回 0 使用absint()

$item_qty_refunded = $order->get_qty_refunded_for_item( $item_id ); // Get the refunded amount for a line item.

$item_total_refunded = $order->get_total_refunded_for_item( $item_id ); // Get the refunded amount for a line item.

暫無
暫無

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

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