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

发布于 2025-01-12 07:58:43 字数 1263 浏览 1 评论 0原文

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

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

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

I want to remove the refund row from the WooCommerce order details table

Existing code copied from /order/order-details.php WooCommerce template file:

<?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 
    }
?>

For this I use the following filter hook:

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 );

This does not give any error messages, but also not the desired result. The row is not deleted. Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2025-01-19 07:58:43

当我们查看 /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 );

When we look at /includes/class-wc-order.php in detail we see the following function is used in WooCommerce for adding the total refunds row(s).

/**
 * 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() ) ),
            );
        }
    }
}

Since an order can consist of several refunds, 'refund_' . $id is used opposite 'refund'


So to remove it, you have to use a loop. So you get:

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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文