将优惠券名称和百分比添加到 WooCommerce 查看订单详细信息和电子邮件通知
Add coupon names and percentage to WooCommerce view order details and email notifications
我用 来激发我的代码片段。
如果优惠券类型是百分比折扣,我正在尝试扩展代码段以在括号中包含优惠券百分比金额。
它应该是这样的:
这是我的尝试。如果这是正确的任何想法:
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();
if( $applied_coupons->discount_type == 'percent'){
// Get applied coupon percentge
$applied_coupons_percentage = $applied_coupons->coupon_amount;
}
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __('Applied coupons:', 'woocommerce'),
'value' => implode( ', ', $applied_coupons '<p> ({$applied_coupons_percentage}%)</p>' ),
);
}
}
return $new_total_rows;
}
我用它来显示购物车页面上的优惠券百分比。我试图将其合并到上面的代码片段中:
function my_coupon_percentage_cart($value, $coupon)
{
if($coupon->discount_type == 'percent' && !empty($coupon->coupon_amount))
{
$amt = "<br><br><p><em><strong>{$coupon->coupon_amount}% OFF on Your Order</strong></em></p>";
}
return $value.$amt;
}
add_filter('woocommerce_cart_totals_coupon_html','my_coupon_percentage_cart',10,2);
除了你的代码有错误之外,它还包含一些过时的代码
get_used_coupons()
自 3.7.0 起已弃用 - 替换为更好命名的方法以反映返回的实际数据。
$coupon->discount_type
已替换为 $coupon->get_discount_type()
$coupon->coupon_amount
已替换为 $coupon->get_amount()
所以你得到:
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;
$new_total_rows = []; // Initializing
foreach( $total_rows as $key => $total ) {
$new_total_rows[$key] = $total;
if ( $key == 'discount' ) {
// Get used coupon codes only
$coupon_codes = $order->get_coupon_codes();
// Loop through WC_Order_Item_Coupon objects
foreach ( $coupon_codes as $index => $coupon_code ) {
// Get an instance of the WC_Coupon Object
$coupon = new WC_Coupon( $coupon_code );
// Discount type = percent & amount NOT empty
if ( $coupon->get_discount_type() == 'percent' && ! empty ( $coupon->get_amount() ) ) {
$coupon_codes[$index] = $coupon_code . ' (' . $coupon->get_amount() . '%)';
}
}
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __( 'Applied coupons:', 'woocommerce' ),
'value' => implode( ', ', $coupon_codes ),
);
}
}
return $new_total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );
我用
如果优惠券类型是百分比折扣,我正在尝试扩展代码段以在括号中包含优惠券百分比金额。
它应该是这样的:
这是我的尝试。如果这是正确的任何想法:
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();
if( $applied_coupons->discount_type == 'percent'){
// Get applied coupon percentge
$applied_coupons_percentage = $applied_coupons->coupon_amount;
}
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __('Applied coupons:', 'woocommerce'),
'value' => implode( ', ', $applied_coupons '<p> ({$applied_coupons_percentage}%)</p>' ),
);
}
}
return $new_total_rows;
}
我用它来显示购物车页面上的优惠券百分比。我试图将其合并到上面的代码片段中:
function my_coupon_percentage_cart($value, $coupon)
{
if($coupon->discount_type == 'percent' && !empty($coupon->coupon_amount))
{
$amt = "<br><br><p><em><strong>{$coupon->coupon_amount}% OFF on Your Order</strong></em></p>";
}
return $value.$amt;
}
add_filter('woocommerce_cart_totals_coupon_html','my_coupon_percentage_cart',10,2);
除了你的代码有错误之外,它还包含一些过时的代码
get_used_coupons()
自 3.7.0 起已弃用 - 替换为更好命名的方法以反映返回的实际数据。$coupon->discount_type
已替换为$coupon->get_discount_type()
$coupon->coupon_amount
已替换为$coupon->get_amount()
所以你得到:
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;
$new_total_rows = []; // Initializing
foreach( $total_rows as $key => $total ) {
$new_total_rows[$key] = $total;
if ( $key == 'discount' ) {
// Get used coupon codes only
$coupon_codes = $order->get_coupon_codes();
// Loop through WC_Order_Item_Coupon objects
foreach ( $coupon_codes as $index => $coupon_code ) {
// Get an instance of the WC_Coupon Object
$coupon = new WC_Coupon( $coupon_code );
// Discount type = percent & amount NOT empty
if ( $coupon->get_discount_type() == 'percent' && ! empty ( $coupon->get_amount() ) ) {
$coupon_codes[$index] = $coupon_code . ' (' . $coupon->get_amount() . '%)';
}
}
// Insert applied coupon codes in total lines after discount line
$new_total_rows['coupon_codes'] = array(
'label' => __( 'Applied coupons:', 'woocommerce' ),
'value' => implode( ', ', $coupon_codes ),
);
}
}
return $new_total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );