If you are offering multiple coupons it can be useful to know which of them was used in an order. This code adds the coupon code to the order details and displays them in a separate row in the emails sent to the customer and admin.
/**
* Add coupon code to order details and emails
*/
// Shows the coupon code used on the order details page and in email sent to customer and admin.
// https://forums.classicpress.net/t/how-to-include-extra-fields-in-classic-commerce-emails/2607/4
add_filter('woocommerce_get_order_item_totals', 'cc_add_coupon_codes_to_order_item_totals', 10, 3);
function cc_add_coupon_codes_to_order_item_totals($rows, $order, $tax_display) {
// Have any coupons been used in $order?
if(! $order->get_used_coupons()) {
return $rows;
}
$new_rows = [];
foreach($rows as $key => $total) {
$new_rows[$key] = $total;
if($key === 'discount') {
$coupons = $order->get_used_coupons();
// Adds row to show coupon codes used in $order. Displays after discount row.
$new_rows['coupon_codes'] = [
'label' => __('Coupons used:', 'classic-commerce'),
'value' => implode(', ', $coupons),
];
}
}
return $new_rows;
}
View Raw
Code ID: 92710