Creating a custom template

Although the WooCommerce Print Address Labels settings plugin comes with many settings to allow for easy modification of the address label contents, you may require a more advanced layout or more details of the order. For this purpose, the plugin is built with a template system. To make use of this template system, you have to copy the template files to your (child) theme to prevent plugin updates from overwriting the changes you made. This article explains how to do that.

The first step in creating a custom label template is to copy the files from the plugin to your child theme. If you do not have a child theme, you can also copy the files to your regular theme folder, but since themes also get updated every once in a while, a child theme (which will never get updated automatically) is the safest place to put them.

Copy all the files from the plugin folder:

wp-content/plugins/woocommerce-address-labels/templates/

to your (child) theme (replace ‘yourtheme’ by your theme folder):

wp-content/themes/yourtheme/woocommerce/labels/

Many WooCommerce enabled themes will already have the /woocommerce folder – if not, you will need to create it. The /labels folder is specific for this plugin and you will always need to create this folder yourself. This is where the plugin will look for the look for the label template. After copying all the files from the plugin template to this folder, the structure will look something like this:

Custom address label template folder structure
Custom address label template folder structure

Now you are ready to start customizing your address labels!

Now that you have copied the template files to your child theme, they are safe from plugin updates. Simply open the file wclabels-label-template.php (or the accompanying stylesheet) in a text editor and start customizing!

tip! Check the ‘Enable preview’ option in the settings, that way you can more easily debug the layout in your browser (view source or inspect element) while customizing.

 

Page structure

You will see that the template only affects the contents of the single label. This template is placed in a table to evenly distribute the labels over each page. It is not possible to customize this table (apart from any CSS adjustments, see the default template stylesheet for the corresponding CSS selectors). However, if you want to know how exactly this is built up, you can find the page template in woocommerce-address-labels/includes/wclabels-page-template.php.

Example: Creating a two column layout

This simple example illustrates how to create a two-column layout with a return address in the left column and the label data (as configured in the plugin settings) in the right column. Note that in this example, all the styles have been put inline, but you can also put them in the stylesheet and use CSS classes if you want to keep the html cleaner

<table style="width:100%">
	<tr>
		<td style="vertical-align: top; font-size: 8pt; width: 30%; padding-left: 7mm;">
			<b>Return address:</b><br>
			Some Company<br>
			123 Main Street<br>
			Anytown, US 12345<br>
		</td>
		<td valign="middle">
			<?php echo $label_data; ?>
		</td>
	</tr>
</table>

You can also output more order data using the woocommerce $order object (using PHP). Instead of the return address in the left column, the following example prints a list of items in the order:

<table style="width:100%">
	<tr>
		<td style="vertical-align: top; font-size: 8pt; width: 50%; padding-left: 7mm;">
			<ul style="list-style: none; margin: 0; padding:0;">
			<?php $items = $order->get_items(); if( sizeof( $items ) > 0 ) : foreach( $items as $item_id => $item ) : ?>
			<li><?php echo $item['qty'];?>x <?php echo $item['name']; ?></li>
			<?php endforeach; endif; ?>
			</ul>
		</td>
		<td style="vertical-align: middle;" >
			<?php echo $label_data; ?>
		</td>
	</tr>
</table>