且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Woocommerce 3 中获取退款订单和退款订单商品详情

更新时间:2023-11-30 12:54:58

要获得退款订单,您可以使用一些专用的 WC_Order 方法,例如以下以 Item Id 作为参数的方法:

To get refunded orders you could use some dedicated WC_Order methods like the following ones that have Item Id as argument:

$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.

您可以访问一个数组 WC_Order_Refund 此订单的对象使用 get_refunds() 方法:

You can access an array WC_Order_Refund Objects for this order using get_refunds() method:

  • For each refund, you can use the WC_Order_Refund methods.
  • For each refund, you can access items using WC_Abstract_Order method get_items() that will give you the refunded items for the current Order refund.
  • Each refund "line" item is a WC_Order_Item_Product (with negative values in general) see this related answer: Get Order items and WC_Order_Item_Product in WooCommerce 3

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

So you can use the following example code:

// 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)
}

相关答案: