从 WooCommerce 订单详细信息中删除退款行 table

Remove refund row(s) from WooCommerce order details table

我想从 WooCommerce 订单详细信息中删除退款行 table

/order/order-details.php WooCommerce 模板文件复制的现有代码:

<?php 
    foreach ( $order->get_order_item_totals() as $key => $total ) { 
        ?>
            <tr>
                <th class="row" scope="row"><?php echo esc_html( $total['label'] ); ?></th>
                <td class="row" scope="row"><?php echo ( 'payment_method' === $key ) ? esc_html( $total['value'] ) : wp_kses_post( $total['value'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></td>
            </tr>
        <?php 
    }
?>

为此,我使用了以下过滤器钩子:

function customize_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
    unset($total_rows['refund']);
    return $total_rows;
} 
add_filter( 'woocommerce_get_order_item_totals', 'customize_woocommerce_get_order_item_totals', 10, 3 );

这不会给出任何错误消息,但也不是预期的结果。该行未被删除。有什么建议吗?

当我们详细查看 /includes/class-wc-order.php 时,我们会看到 WooCommerce 中使用了以下函数来添加总退款行。

/**
 * Add total row for refunds.
 *
 * @param array  $total_rows  Total rows.
 * @param string $tax_display Tax to display.
 */
protected function add_order_item_totals_refund_rows( &$total_rows, $tax_display ) {
    $refunds = $this->get_refunds();
    if ( $refunds ) {
        foreach ( $refunds as $id => $refund ) {
            $total_rows[ 'refund_' . $id ] = array(
                'label' => $refund->get_reason() ? $refund->get_reason() : __( 'Refund', 'woocommerce' ) . ':',
                'value' => wc_price( '-' . $refund->get_amount(), array( 'currency' => $this->get_currency() ) ),
            );
        }
    }
}

由于一个订单可以包含多次退款,因此'refund_' . $id'refund'

相反

所以要删除它,你必须使用循环。所以你得到:

function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
    // Get the Order refunds (array of refunds)
    $order_refunds = $order->get_refunds();

    // NOT empty
    if ( ! empty( $order_refunds) ) {
        // Unset
        foreach ( $order_refunds as $id => $refund ) {
            unset( $total_rows[ 'refund_' . $id ] );
        }
    }

    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );