在Woocommerce 3中获取退款的订单和退款的订单项详细信息

时间:2018-09-25 04:43:14

标签: php wordpress methods woocommerce orders

我看到当我查看订单时,如果没有整个订单,它将显示已退款的特定商品。

是否可以使用WC_Order_Item_Product查找商品是否已退款?此外,有没有办法获得显示在订单视图中项目下方的折扣金额?

我目前正在手动进行操作,但是如果已经有它的功能,我宁愿使用它。

2 个答案:

答案 0 :(得分:3)

要获得订单退款,您可以使用一些专用的WC_Order methods,例如以下以商品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.

您可以使用WC_Order_Refund方法按此顺序访问数组get_refunds()对象:

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

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

相关答案:

答案 1 :(得分:0)

如果您使用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.