Skip to content

Instantly share code, notes, and snippets.

@MrJoshFisher
Created May 12, 2023 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrJoshFisher/952dcd208a8086d0044d527bcafccf34 to your computer and use it in GitHub Desktop.
Save MrJoshFisher/952dcd208a8086d0044d527bcafccf34 to your computer and use it in GitHub Desktop.
[Add Coupons To Order And Email] #woocommerce #wordpress
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