在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对象的数组:

  • 对于每次退款,您可以使用WC_Order_Refund方法。
  • 对于每次退款,您可以使用WC_Abstract_Order方法访问商品get_items(),该方法将为您提供当前订单退款的退款商品。
  • 每个退款"行"项目都是一个WC_Order_Item_Product(通常为负值(请参阅此相关答案:在WooCommerce 3中获取订单项目和WC_Order_Item_Product

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

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

相关答案:

  • 如何获取WooCommerce订单详细信息
  • 在WooCommerce 3中获取订单项目和WC_Order_Item_Product
  • WooCommerce - 获取订单项目价格和数量。

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

相关内容

最新更新