Trying to put the description of applied coupon into the "New Order" admin email with not success
So far trying to use this code I put into functions.php
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admins and when there at least 1 coupon in the order
if ( ! $sent_to_admin && count($order->get_items('coupon') ) == 0 ) return;
foreach( $order->get_items('coupon') as $coupon ){
$coupon_codes[] = $coupon->get_code();
$coupon_desc[] = $coupon->get_description();
}
// For one coupon
if( count($coupon_codes) == 1 ){
$coupon_code = reset($coupon_codes);
echo '<p>'.__( 'Coupon Used: ').$coupon_code.'<p>';
$coupon_des = reset($coupon_desc);
echo '<p>'.__( 'Coupon Description: ').$coupon_des.'<p>';
}
// For multiple coupons
}
Unless it doesn't produce any error, I can't see the coupon description into the "New Order" admin email
Somebody can help me? Thank you in advance
There are some mistakes in your code… Try the following instead:
add_action( 'woocommerce_email_order_details', 'display_applied_coupons', 10, 4 );
function display_applied_coupons( $order, $sent_to_admin, $plain_text, $email ) {
// Only for admins and when there at least 1 coupon in the order
if ( $sent_to_admin && count( $order->get_coupons() ) > 0 ) {
$html = '<div class="coupon-items">
<h2>' . __( "Used coupons" ) . '<h2>
<table class="td" cellspacing="0" cellpadding="6" border="1"><tr>
<th>' . __("Code") . '</th>
<th>' . __("Description") . '</th>
</tr>';
foreach( $order->get_coupons() as $item ){
$code = $item->get_code();
$coupon = new WC_Coupon($code);
$html .= '<tr>
<td>' . ucfirst( $code ) . '</td>
<td>' . $coupon->get_description() . '</td>
</tr>';
}
$html .= '</table><br></div>';
// The CSS styling
$css = '<style>
.coupon-items table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
.coupon-items table th, table.tracking-info td{text-align: left; border-top-width: 4px;
color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
.coupon-items table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
</style>';
// The Output CSS + HTML
echo $css . $html;
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.