WooCommerce Email Template Change | How To Add Client Billing Phone Number

How To Fix an issue in WooCommerce

I work with a client using WooCommerce as his e-commerce platform and he very often wants little tweaks and adjustments that are making his life easier while working and implementing additional functionality to the WooCommerce plugin. On the other hand as I’m making those little tweaks for him I see how difficult it is to find relevant and targeted answers to exactly meet his expectations. Thats why I will try to share answers I found to be correct and usefull in my daily work.

THE ISSUE:

My client wanted, that the email with the new order he becomes will have the phone number also under the billing / shipping address displayed, not only at the order meta section where the clients email and phone is displayed. He didn’t want to each time have to separately copy over the phone number and the shipping address because that was one thing too much to do, and he’s completely right.

THE APPROACH:

I do not know much about actions, hooks, hooking into actions etc., where to find them, where to edit them so I did need a simple resolution which would let me hard-paste the code which returns the billing phone number inside of a few places and templates, with no additional php or coding knowledge.

THE FIX:

After a long search I found out that in WooCommerce the correct code which is returning the billing phone is this function:

function show_order_billing_phone($order) {

if ($order->billing_phone) {

echo $order->billing_phone;

}

} ?>

This few lines of code output the billing phone number in WooCommerce. Now we can simply add the function to any place we need and call it within the template itself. This won’t require any hooks. To call the function we just need to add this line, so the entire code will look like this:

<?php

function show_order_billing_phone($order) {

if ($order->billing_phone) {

echo $order->billing_phone;

}

show_order_billing_phone($order);

} ?>

As you see calling a function is very simple and it will return us the phone number of the client.

AFTER September 2015 I had a small issue with that and upgraded this by outputting this code to simplyfy it:

<?php
function show_order_billing_phone($order) {
echo $order->billing_phone;
}
show_order_billing_phone($order);
?>

HOW TO ADD THIS TO INVOICE EMAIL FOR ADMIN:

When someone orders in Your shop you get a notification about that from WooCommerce. Since my client requested that this should appear in the email he is becomming under the adress section I did need to make changes in the admin-new-order.php file from the woocommerce templates, which can be found on Your FTP server under:

yourwebsite.com/wp-content/plugins/woocommerce/templates/emails/

once you enter the admin-new-order.php file paste the function code including the call to display the function right behind :

<?php do_action( ‘woocommerce_email_customer_details’, $order, $sent_to_admin, $plain_text ); ?>

and before:

<?php do_action( ‘woocommerce_email_footer’ ); ?>

Or in any other place you need the phone number to be returned.

VERY IMPORTANT!!!!

Remember that changing any WooCommerce template file will be OVERWRITTEN during the next patch. To prevent that you need to copy the file into Your themes folder and it will be prioritized by WooCommerce plugin over the original template files. So once you have any fix done copy the file from:

yourwebsite.com/wp-content/plugins/woocommerce/templates/emails/ to

yourwebsite.com/wp-content/themes/themename/woocommerce/emails/

(if the file you changed was one of the email templates). Once you do that the file will remain changed even if you do the neccesary WooCommerce updates. A good idea is to double check if something major did change in the file you did edit, but for the most part WooCommerce tries not to make hard changes in the template files too often, so most likely you are secure for some time.

2 comments

  1. Hi

    Why not use the hook in the template?
    When there is an action like ‘woocommerce_email_after_order_table’ or ‘woocommerce_email_order_meta’, you could just use that to register your function.

    That way you don’t have to overwrite the template file at all. Much cleaner solution is it not?

  2. Hi Halvard thank you for telling this! I wrote this post for people who were seeking this simple solution. Sadly I myself also doesn’t know how to “use hooks” in the template, and I don’t know how to register a function within that. That’s the problem 🙂

Leave a comment

Your email address will not be published. Required fields are marked *