我想從 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 );
這不會給出任何錯誤訊息,但也不會給出預期的結果。該行不會被洗掉。有什么建議嗎?
uj5u.com熱心網友回復:
當我們詳細查看/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 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/439951.html
標籤:php WordPress woocommerce 命令
下一篇:WooCommerce不會觸發自定義訂單狀態的“woocommerce_order_status_changed”掛鉤
