This snippet looks at if there are any coupon codes used in the order.
If there are, it fetches the coupon coupon type, name and the amount.
If the coupon code type is a percentage based discount, a percentage symbol is appended to the amount.
If the coupon code type is a fixed based discount, the currency symbol prefixes the amount.
Any codes are then shown in a formatted table which has had CSS applied to make it match the rest of the tables shown in the email.
Both the discount name and amount is shown beneath the main order details table.
/**
* Snippet Name: WooCommerce Show Coupon Code Used In Emails
* Snippet Author: ecommercehints.com
*/
add_action( 'woocommerce_email_after_order_table', 'ecommercehints_show_coupons_used_in_emails', 10, 4 );
function ecommercehints_show_coupons_used_in_emails( $order, $sent_to_admin, $plain_text, $email ) {
if (count( $order->get_coupons() ) > 0 ) {
$html = '
Used coupons
Coupon Code
Coupon Amount
';
foreach( $order->get_coupons() as $item ){
$coupon_code = $item->get_code();
$coupon = new WC_Coupon($coupon_code);
$discount_type = $coupon->get_discount_type();
$coupon_amount = $coupon->get_amount();
if ($discount_type == 'percent') {
$output = $coupon_amount . "%";
} else {
$output = wc_price($coupon_amount);
}
$html .= '
' . strtoupper($coupon_code) . '
' . $output . '
';
}
$html .= '
';
$css = '';
echo $css . $html;
}
}
One Response
Great code thanks!