Updated - 处理零值折扣
以下代码将在订单总计行中的“折扣”行之后显示订单所应用的优惠券:
add_filter( 'woocommerce_get_order_item_totals', 'add_coupons_codes_line_to_order_totals_lines', 10, 3 );
function add_coupons_codes_line_to_order_totals_lines( $total_rows, $order, $tax_display ) {
// Exit if there is no coupons applied
if( sizeof( $order->get_used_coupons() ) == 0 )
return $total_rows;
$new_total_rows = []; // Initializing
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
if( $key == 'discount' ){
// Get applied coupons
$applied_coupons = $order->get_used_coupons();
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __('Applied coupons:', 'woocommerce'),
'value' => implode( ', ', $applied_coupons ),
);
}
}
return $new_total_rows;
}
显示在客户订单视图上,并使用 2 张优惠券:
附加代码版本:处理零折扣金额的已应用优惠券,请使用以下命令:
add_filter( 'woocommerce_get_order_item_totals', 'add_coupons_codes_line_to_order_totals_lines', 10, 3 );
function add_coupons_codes_line_to_order_totals_lines( $total_rows, $order, $tax_display ) {
$has_used_coupons = sizeof( $order->get_used_coupons() ) > 0 ? true : false;
// Exit if there is no coupons applied
if( ! $has_used_coupons )
return $total_rows;
$new_total_rows = []; // Initializing
$applied_coupons = $order->get_used_coupons(); // Get applied coupons
foreach($total_rows as $key => $total ){
$new_total_rows[$key] = $total;
// Adding the discount line for orders with applied coupons and zero discount amount
if( ! isset($total_rows['discount']) && $key === 'shipping' ) {
$new_total_rows['discount'] = array(
'label' => __( 'Discount:', 'woocommerce' ),
'value' => wc_price(0),
);
}
// Adding applied coupon codes line
if( $key === 'discount' || isset($new_total_rows['discount']) ){
// Get applied coupons
$applied_coupons = $order->get_used_coupons();
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __('Applied coupons:', 'woocommerce'),
'value' => implode( ', ', $applied_coupons ),
);
}
}
return $new_total_rows;
}
在电子邮件通知中显示具有 0 折扣的优惠券:
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。