Emails

WooCommerce Show Coupon Code Used In Emails

WooCommerce email showing coupon code used

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

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 = '<div class="used-coupons">
        <h2>Used coupons<h2>
        <table class="td" cellspacing="0" cellpadding="6" border="1"><tr>
        <th>Coupon Code</th>
        <th>Coupon Amount</th>
        </tr>';

        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 .= '<tr>
                <td>' . strtoupper($coupon_code) . '</td>
                <td>' . $output . '</td>
            </tr>';
        }
        $html .= '</table><br></div>';

        $css = '<style>
            .used-coupons table {
				width: 100%;
				font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
                color: #737373;
				border: 1px solid #e4e4e4;
				margin-bottom:8px;
			}
            .used-coupons table th, table.tracking-info td {
			text-align: left;
			border-top-width: 4px;
            color: #737373;
			border: 1px solid #e4e4e4;
			padding: 12px;
			}
            .used-coupons table td {
			text-align: left;
			border-top-width: 4px;
			color: #737373;
			border: 1px solid #e4e4e4;
			padding: 12px;
			}
        </style>';

        echo $css . $html;
    }
}

Snippet Benefits

  • Remind users and admins how exactly the customer gained the order discount by showing the coupon code used in the post-purchase emails.

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.

26027
WELCOME OFFER