You may need to show coupon details in the WooCommerce order email. This option is not available by default. We can use woocommerce_email_after_order_table
action hook to show used coupon details in order email. This will show coupon details after the order table in the order email.
We can get coupon object using WC_Coupon
class easily. Coupon is a custom post type. So we can get coupon details using get_post
method as well.
function careless_add_coupon_to_email( $order ) {
//get array of used coupons
$coupons = $order->get_coupon_codes();
//no coupon used
if ( !$coupons ) {
return;
}
foreach ( $coupons as $coupon_code ) {
//get coupon object
$coupon = new WC_Coupon( $coupon_code );
echo '<p class="coupon-used"><span class="coupon-name"><b>Coupon: ' . $coupon->get_code() . '</b></span></p>';
echo '<p><span class="coupon-description">' . $coupon->get_description() . '</span></p';
}
}
add_action( 'woocommerce_email_after_order_table', 'careless_add_coupon_to_email', 20 );
Code goes in the functions.php
file of your active theme.