【问题标题】:Get refunded orders and refunded order items details in Woocommerce 3在 Woocommerce 3 中获取退款订单和退款订单商品详细信息
【发布时间】:2019-02-28 15:16:03
【问题描述】:

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

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

我目前是手动做的,但如果已经有它的功能,我宁愿使用它。

【问题讨论】:

  • 用那个重要的细节更新了答案:您可以使用$item->get_meta('_refunded_item_id'); 获取已退款的原始商品ID

标签: php wordpress methods woocommerce orders


【解决方案1】:

要获得退款订单,您可以使用一些专用的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.

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

所以你可以使用下面的示例代码:

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

相关答案:

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-28
      • 2020-01-11
      • 2019-07-08
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      相关资源
      最近更新 更多