在Woocommerce 3中获取退款的订单和退款的订单项详细信息 [英] Get refunded orders and refunded order items details in Woocommerce 3

查看:466
本文介绍了在Woocommerce 3中获取退款的订单和退款的订单项详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I see when I look at an order it will show the specific item that was refunded if the whole order wasn't.

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

Is there a way to find using the WC_Order_Item_Product if an item was refunded? Also, is there a way to get the discount amount that shows up below the item in the order view?

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

I'm currently doing it manually but if there is a function for it already, I would rather use it.

推荐答案

要获取订单退款,您可以使用一些专用的

To get the order refunds you you use some dedicated WC_Order methods like the following ones that have the 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:

  • 对于每次退款,您都可以使用 WC_Order_Refund 方法.
  • 对于每笔退款,您都可以使用WC_Abstract_Order方法 ,将为您提供当前订单退款的退款项目.
  • 每个退款行"项目都是 WC_Order_Item_Product (通常具有负值)请参见以下相关答案:
  • 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
    }
}

要获取显示在管理员订单编辑页面中的订单商品的折扣值,您将使用以下代码:

To get the order items discounted values that appear in admin order edit pages you will use the following code:

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

相关答案:

  • How to get WooCommerce order details
  • Get Order items and WC_Order_Item_Product in Woocommerce 3
  • Woocommerce - Getting the order item price and quantity.

这篇关于在Woocommerce 3中获取退款的订单和退款的订单项详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆