How to include extra fields in Classic Commerce emails?

Another question, (do not know if this should be a new thread or not)

Is it possible to include extra fields, specifically the coupon code used to buy the product, in the emails that are sent when order placed. The code is shown in the order
If answer is yes, how would I do this.

1 Like

New topic, new thread. :+1:

1 Like

Assuming CC uses the same filters as woocommerce, have you tried them?

2 Likes

Yes, CC uses the exact same filters as WC 3.5.3.

You could try this snippet I used on an old site:

add_filter( 'woocommerce_get_order_item_totals', 'cc_add_coupon_codes_to_order_item_totals', 10, 3 );
function cc_add_coupon_codes_to_order_item_totals( $rows, $order, $tax_display ) {
    // Have any coupons been used in $order?
    if( ! $order->get_used_coupons() ) {
        return $rows;
	}

    $new_rows  = [];

    foreach( $rows as $key => $total ) {
        $new_rows[$key] = $total;

        if( $key === 'discount') {
            $coupons = $order->get_used_coupons();
            // Adds row to show coupon codes used in $order. Displays after discount row.
            $new_rows['coupon_codes'] = [
                'label'	=> __('Coupons used:', 'classic-commerce'),
                'value'	=> implode( ', ', $coupons ),
            ];
        }
    }

    return $new_rows;
}

Place the code in functions.php or, ideally, a MU plugin.

Not fully tested so use on a staging site first.

I’d also recommend installing this plugin Preview E-mails for WooCommerce so that you can preview order emails using data from existing orders. It avoids the need to send an actual email. Quite handy.

Let me know how you get on. If it works, it’s another one for the list of snippets @ozfiddler.


Note that get_used_coupons was deprecated in WC3.7 and replaced with get_coupon_codes. I mention this for information only as it doesn’t affect CC.

1 Like

Thanks Tim!

I have got a few “add to email” type snippets but only for fixed content. There is also a snippet as part of “add extra fields to checkout process” that puts the extra field into an email. But nothing yet that gets an existing CC field.

I’ll test it out and add it in.

2 Likes

Works for me! Now added in…

https://classiccommerce.cc/docs/snippets/add-coupon-code-to-order-details-and-emails/

:+1:

3 Likes

Looking good Oz! :+1:t2:

Did the above code snippet work for you @Mark?

I did not get chance yet .Hopefully to-morrow
Is it possible to change the email subject also? As I now have 250 emails with subject [Dear Santa]: New order

If you go to Settings > Emails and then click Manage at the end of the “new order” line you can tweak the fields. Hover over the question mark and it will show you the available placeholders.

Only options seem to be Site title, Date and order number
In admin area the orders summary page only adds name and price.

The reason I ask is as there are several campaigns running each with its own coupon code I have not found an easy way to see a summary that includes any way to differentiate the campaigns.
This is highish volume with low profit per item so time spent opening orders and emails just to check is not cost effective.

I can export the orders and then check in excel but this is a bit tiresome especially when a client rings twice a day to see how his new ad, with its own code, is doing.
Especially on FaceBook ppc as it is easy to spend more in advertising than you are making on the products sold

Ah, you want to track coupon use.

You could maybe use this snippet:

That looks like it sends a separate email with coupon code.

Otherwise I’m sure someone could come up with a snippet to add the coupon code into the subject line. Tim’s has added it to the email body so shouldn’t be hard to drop it into the subject as well.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.