[英]Split discount (coupon) row into multiple deducted cost rows in Woocommerce order totals table
如果使用了 2 張折扣券,它會在 Woocommerce 訂單總計表中顯示 2 張優惠券的總和,而我想分別顯示每張優惠券的扣除成本。
例如插入了2張優惠券,目前顯示:
我想將其更改為:
優惠券1(優惠券代碼):50美元
優惠券2(優惠券代碼):$ 50
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
}
關於如何改變這一點的任何建議?
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;
}
沒有必要通過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_coupon_codes() as $coupon_code ) {
// Get the WC_Coupon object
$coupon = new WC_Coupon( $coupon_code );
// New rows
$new_rows[$coupon_code] = array(
'label' => __( 'Coupon: ', 'woocommerce' ) . $coupon_code,
'value' => wc_price( $coupon->get_amount() ),
);
}
}
}
// Overwrite
$total_rows = $new_rows;
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.