Created
May 12, 2023 10:23
-
-
Save MrJoshFisher/952dcd208a8086d0044d527bcafccf34 to your computer and use it in GitHub Desktop.
[Add Coupons To Order And Email] #woocommerce #wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
$coupons = array(); | |
foreach($applied_coupons as $coupon) { | |
if (strpos($coupon, 'wc_points_redemption') !== false) { | |
array_push($coupons, 'Point Discount'); | |
} else { | |
array_push($coupons, $coupon); | |
} | |
} | |
$new_total_rows['coupon_codes'] = array( | |
'label' => __('Applied coupons:', 'woocommerce'), | |
'value' => implode( ', ', $coupons ), | |
); | |
} | |
} | |
return $new_total_rows; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment