A Guide to the WooCommerce wc_get_template Function

What Does the WooCommerce wc_get_template Function Do?

The wc_get_template function can be used to display the contents of template files.

The function allows you to pass values into the templates which can then be displayed as part of the template.

The function also allows templates to be over ridden either in themes or plugins.

A Simple Example Using the wc_get_template Function

Let’s take a look at an example where we use the wc_get_template function to display the  WooCommerce “My Account” dashboard

wc_get_template(
    'myaccount/dashboard.php',
    array(
        'current_user' => get_user_by( 'id', get_current_user_id() )
    )
)

If we add the code above to a PHP file on WordPress site that has WooCommerce installed, then we should see the output below

The function takes the file path supplied as the first argument and then renders the file using the value passed in the array to the function’s second argument.

You can look at the code for the myaccount/dashboard.php  template here

https://github.com/woocommerce/woocommerce/blob/master/templates/myaccount/dashboard.php

There’s quite a lot going on in that file but it should give you an ide a of how a template file could work, we will look at a simpler example later in this article

How to Override the Template using the wc_get_template Function

The wc_get_template function gives you the ability to override templates, this is the same functionality that we would use to override templates in a child theme.

If we wanted to override the template in the example above then we would need to create a directory named “woocommerce” in our theme or child theme and then create a directory under that named “myaccount” and add copy the “dashboard.php” file into there.

We could then make changes to the “dashboard.php” file inside our theme folder and the wc_get_template function would render the changes we had made in the template file.

You could do this with your own files as well.

Using the wc_get_template Function with a Plugin

When writing a WooCommerce plugin the wc_get_template function can be used to allow users of our plugin to change the output of the  plugin by creating their own template files

Let’s imagine we have written a plugin that adds a value to the meta data of all orders and displays it on the order admin screen, here’s the code for the plugin

<?php
/**
 * Plugin Name: HWN Order Meta
 */

add_action('woocommerce_checkout_create_order', 'hwn_add_meta_data_to_order', 20, 2);
function hwn_add_meta_data_to_order( $order, $data ) {
    $order->update_meta_data( '_custom_meta_key', 'value' );
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'hwn_display_custom_meta_key' );
function hwn_display_custom_meta_key( $order ){
    $value = get_post_meta( $order->id, '_custom_meta_key', true );

    wc_get_template( 'ordermetaplugin/template.php',
        array('Title'    => "Custom Meta",
        'Value' => $value), '', trailingslashit(__DIR__) );
}

As you can see we use the wc_get_template function to output the meta value to the screen, in the function we pass the path “ordermetaplugin/template.php” as the plugin path and we pass two key value pairs to the array as a second argument.

For the first pair the key is “Title” and the value is “Custom Meta”, for the second pair the key is “Value” and we pass the value we have extracted form the meta data as the values.

In this example we also pass a third and fourth value to the  wc_get_template function, the third argument is a blank string, but we then pass the plugin’s directory as the fourth argument, which will be  the default path that WooCommerce should use to look for the template.

This means that when our plugin is installed it will use the template file that it finds in the “ordermetaplugin” directory in the plugin directory, but, if the user wishes to replace the template with a template file of their choosing they can do so by copying a file named “template.php” to the “wooccomerce/ordermetaplugin” folder inside their theme or child theme folder.

If this is the contents of the  “template.php” file in our plugin

<strong><?=$Title?>:</strong> <?=$Value?>

then the following will be out put the screen

We can see how the key value pairs we passed into the wc_get_template as the second argument are displayed by the template.

If the store owner with our plugin installed then created a file in the “wooccomerce/ordermetaplugin” directory and added the following code

<strong><?=$Title?>:</strong> <?=$Value?> <strong>PLEASE ENSURE THIS VALUE IS ADDED TO THE PACKING SLIP</strong>

Then the plugin’s output would be changed to

Final Thoughts

The wc_get_template function is used throughout the WooCommerce code base and we can use it in our own code to output mark-up and also to allow any templates we provide to be easily replaced by end users.

Find out more about how to modify WooCommerce
Click here to enroll in our Free WooCommerce Coding Course

If you do have any questions or comments about the wc_get_template function then please don’t hesitate to let us know in the comments.


Warning: Undefined array key "format" in H:\root\home\bravenewworld101-001\www\hardworkingnerd\wp-content\plugins\convertkit\includes\class-convertkit-resource-forms.php on line 153

Leave a Comment