Skip to content

Instantly share code, notes, and snippets.

@corsonr
Created November 8, 2013 13:00
Show Gist options
  • Save corsonr/7370707 to your computer and use it in GitHub Desktop.
Save corsonr/7370707 to your computer and use it in GitHub Desktop.
WooCommerce - display order coupons used in confirmation email and edit order page
add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );
/**
* Add used coupons to the order confirmation email
*
*/
function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {
if ( $is_admin_email ) {
if( $order->get_used_coupons() ) {
$coupons_count = count( $order->get_used_coupons() );
echo '<h4>' . __('Coupons used') . ' (' . $coupons_count . ')</h4>';
echo '<p><strong>' . __('Coupons used') . ':</strong> ';
$i = 1;
$coupons_list = '';
foreach( $order->get_used_coupons() as $coupon) {
$coupons_list .= $coupon;
if( $i < $coupons_count )
$coupons_list .= ', ';
$i++;
}
echo '<p><strong>Coupons used (' . $coupons_count . ') :</strong> ' . $coupons_list . '</p>';
} // endif get_used_coupons
} // endif $is_admin_email
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
/**
* Add used coupons to the order edit page
*
*/
function custom_checkout_field_display_admin_order_meta($order){
if( $order->get_used_coupons() ) {
$coupons_count = count( $order->get_used_coupons() );
echo '<h4>' . __('Coupons used') . ' (' . $coupons_count . ')</h4>';
echo '<p><strong>' . __('Coupons used') . ':</strong> ';
$i = 1;
foreach( $order->get_used_coupons() as $coupon) {
echo $coupon;
if( $i < $coupons_count )
echo ', ';
$i++;
}
echo '</p>';
}
}
@mrteecee
Copy link

Some great code, but should be looking much nicer ;)
(BTW: the 2 printout is intended to be different? If they should be the same, than the first function could call the secound within the first condition, thus leaving less space for making any mistake editing only one of them if needed...)

add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );

/**
 * Add used coupons to the order confirmation email
 *
 */
function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {
    if (
        $is_admin_email
        && ($coupons = $order->get_used_coupons())
    ) {
        echo sprintf('
        <h4>%s (%d)</h4>
        <p><strong>%1$s:</strong>
        <p><strong>Coupons used (%2$d) :</strong> %s</p>',
            __('Coupons used'),
            count($coupons),
            implode(', ', $coupons)
        );
    }
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );

/**
 * Add used coupons to the order edit page
 *
 */
function custom_checkout_field_display_admin_order_meta($order) {
    if( $coupons = $order->get_used_coupons() ) {
        echo sprintf('
            <h4>%s (%d)</h4>
            <p><strong>%1$s:</strong>%s</p>',
            __('Coupons used'),
            count($coupons),
            implode(', ', $coupons)
        );
    }
}

@zz5zz
Copy link

zz5zz commented Jan 21, 2017

Doesn't seem to work with latest Woocommerce since $order->get_used_coupons() will never return anything.

@lab21gr
Copy link

lab21gr commented Mar 13, 2017

Great code, really helped me out, but i also added one line of code to show the amount of the coupon used:
echo $order->get_discount_to_display();
I added it right after the "foreach" function

@diwakar4urs
Copy link

diwakar4urs commented Jun 9, 2018

how to send the coupon descritiption to the customer for order mail, can anyone help me on this it is urgent

@swiggle123
Copy link

Is there a way to show the amount of the coupon that was used? Currently using the code below and i want the amount of the coupon to be displayed as well. We use multiple coupons and want the amount for each to be displayed

`add_action( 'woocommerce_email_after_order_table', 'add_payment_method_to_admin_new_order', 15, 2 );

/**

  • Add used coupons to the order confirmation email

*/
function add_payment_method_to_admin_new_order( $order, $is_admin_email ) {

	if( $order->get_used_coupons() ) {

		$coupons_count = count( $order->get_used_coupons() );
	    $i = 1;
	    $coupons_list = '';

	    foreach( $order->get_used_coupons() as $coupon) {
	        $coupons_list .=  $coupon;
	        if( $i < $coupons_count )
	        	$coupons_list .= ', ';
	        $i++;
	    }

	    echo '<p></p>';
	    echo '<p><strong>Credit(s) used (' . $coupons_count . ') :</strong> ' . $coupons_list . '</p>';

	} // endif get_used_coupons

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment