如果使用了2张折扣券,则在Woocommerce订单总计表中显示2张优惠券的总和,而我想分别显示每张优惠券的扣除成本。
例如,插入了2张优惠券,目前它显示:
我想把这个改为:
来自WooCommerce模板文件的路径:order-details.php
foreach ( $order->get_order_item_totals() as $key => $total ) {
?>
<tr>
<th scope="row"><?php echo esc_html( $total['label'] ); ?></th>
<td><?php echo ( 'payment_method' === $key ) ? esc_html( $total['value'] ) : wp_kses_post( $total['value'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></td>
</tr>
<?php
}关于如何改变这个问题有什么建议吗?
发布于 2022-02-06 05:49:18
没有必要通过woocommerce_get_order_item_totals钩子添加HTML标记,因为它涉及表行。
接下来,答案将删除默认折扣行,并将其分割成多行,并包含所需的详细信息。
本答复中使用的函数:
所以你得到了:
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
// Exit if there is no coupons applied
if ( sizeof ( $order->get_coupon_codes() ) == 0 ) return $total_rows;
// Initializing
$new_rows = [];
// Loop through order total lines
foreach ( $total_rows as $total_key => $total_row ) {
// Store current rows, except discount
if ( $total_key != 'discount' ) {
// Store
$new_rows[$total_key] = $total_row;
} else {
// For each coupon
foreach ( $order->get_items('coupon') as $coupon ) {
// New rows
$new_rows[$coupon->get_code()] = array(
'label' => __( 'Coupon: ', 'woocommerce' ) . $coupon->get_code(),
'value' => wc_price( $coupon->get_discount() ),
);
}
}
}
// Overwrite
$total_rows = $new_rows;
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );结果:

发布于 2022-02-05 16:07:31
add_filter('woocommerce_get_order_item_totals', 'woocommerce_get_order_item_totals', 10, 3);
function woocommerce_get_order_item_totals($total_rows, $order, $tax_display) {
// Loop through order items "coupon"
$original_dicount_value = $total_rows['discount']['value'];
$breaked_dicount_value = '';
$wrapp_table_start = '<table style="width:100%">';
$i = 1;
foreach ($order->get_items('coupon') as $item_id => $item) {
// Get the coupon array data
$data = $item->get_data();
$coupon_code = $data['code'];
$coupon_amt = strip_tags(wc_price($data['discount'] + $data['discount_tax']));
$breaked_dicount_value .= "<tr>
<td>coupon$i($coupon_code)</td>
<td>$coupon_amt</td>
</tr>";
$i++;
}
$wrapp_table_end = "</table>";
$total_rows['discount']['value'] = ('' !== $breaked_dicount_value ) ? $wrapp_table_start . $breaked_dicount_value . $wrapp_table_end : $original_dicount_value;
return $total_rows;
}https://stackoverflow.com/questions/70999498
复制相似问题