PDF template action hooks

The template engine of PDF Invoices & Packing slips is built for flexibility, and includes several action hooks where you can output custom content:

Hook Position Arguments
 wpo_wcpdf_after_document_label  After the document label (Invoice, Packing Slip etc.)  $template_type, $order
 wpo_wcpdf_before_order_data  Before the order data (invoice number, order date, etc.)note that this is inside a table, and you should output the data as an html table row/cells  $template_type, $order
 wpo_wcpdf_after_order_data  After the order datanote that this is inside a table, and you should output the data as an html table row/cells  $template_type, $order
 wpo_wcpdf_before_order_details  Before the order details table with all items  $template_type, $order
 wpo_wcpdf_after_order_details  After the order details table  $template_type, $order
 wpo_wcpdf_before_item_meta  Before the item meta (for each item in the order details table)  $template_type, $item, $order
 wpo_wcpdf_after_item_meta  After the item meta (for each item in the order details table)  $template_type, $item, $order

 

If you have never used WordPress action hooks before, this may sound like abracadabra. Luckily it’s not that difficult, although some basic PHP knowledge does help a lot if you’re doing this.

There are two places where you can put the code from the examples below. You can either insert the code into your theme functions / functions.php (read this if you have never edited that file before!) or if you have already created a custom pdf template, you can put it in the template-functions.php file inside your custom template folder.

This will print a custom field (for example ‘delivery_date’) below the regular order data on the packing slip. If you don’t know the name of your custom field, follow this guide: Finding WooCommerce Custom Fields

add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_delivery_date', 10, 2 );
function wpo_wcpdf_delivery_date ($template_type, $order) {
	$document = wcpdf_get_document( $template_type, $order );
	if ($template_type == 'packing-slip') {
		?>
		<tr class="delivery-date">
			<th>Delivery Date:</th>
			<td><?php $document->custom_field('delivery_date'); ?></td>
		</tr>
		<?php
	}
}

Some plugins save the delivery date as a UNIX timestamp, which is a big number like 1483228800. In this case you will need to convert this to human readable format first.

add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_delivery_date', 10, 2 );
function wpo_wcpdf_delivery_date ($template_type, $order) {
    if ($template_type == 'packing-slip') {
        // get the delivery date from the order (WC3.0 or WC2.6)
        $delivery_date = method_exists($order, 'get_meta') ? $order->get_meta('delivery_date',true,'edit') : get_post_meta( $order->id, 'delivery_date', true );
        // convert the delivery date to a human readable format (using the WordPress date format settings)
        $formatted_delivery_date = date_i18n( get_option('date_format'), $delivery_date );
        ?>
        <tr class="delivery-date">
            <th>Delivery Date:</th>
            <td><?php echo $formatted_delivery_date; ?></td>
        </tr>
        <?php
    }
}

This will print a simple text below the order details table on all PDF documents.

add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text', 10, 2 );
function wpo_wcpdf_custom_text ($template_type, $order) {
	?>
	<div class="custom-text">
	If you have any questions about your order, don't hesitate to contact us!
	</div>
	<?php
}

Or restricted to the PDF invoice and BACS payment method:

add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_custom_text', 10, 2 );
function wpo_wcpdf_custom_text ($template_type, $order) {
	$payment_method = method_exists($order, 'get_payment_method') ? $order->get_payment_method() : $order->payment_method;
	if ($template_type == 'invoice' && $payment_method == 'bacs') {
		?>
		<div class="custom-text">
		Thank you for your order. Please send the full amount to our bank account: 123-456-789.
		After we have received your payment, you will receive a notification and we will send you the goods.
		</div>
		<?php
	}
}

Or a text about intra-community supply, restricted to invoices (& proforma invoices & credit notes) without tax:

add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_tax_exempt', 10, 2 );
function wpo_wcpdf_tax_exempt ($template_type, $order) {
	$total_tax = $order->get_total_tax();
	if ( in_array( $template_type, array('invoice', 'proforma', 'credit-note') ) && $total_tax == 0 ) {
		?>
		<div class="tax-exempt">
		Tax free under intra-community supply.
		</div>
		<?php
	}
}

This example prints a comma separated list of categories for each product under the product name (after the item meta).

add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_show_product_categories', 10, 3 );
function wpo_wcpdf_show_product_categories ( $template_type, $item, $order ) {
	// get a comma separated list of categories (category links stripped)
	if (isset($item['product'])) {
		if (function_exists('wc_get_product_category_list')) {
			// WC3.0+
			$category_list = wc_get_product_category_list( $item['product']->get_id() );
		} else {
			$category_list = $item['product']->get_categories();
		}
		echo '<div class="product-categories">Categories: '.strip_tags( $category_list ).'</div>';
	}
}

Calculate a due date 14 days after the invoice date (see the ‘+ 14 days’ bit and strtotime() for reference)

add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_due_date', 10, 2 );
function wpo_wcpdf_due_date ($template_type, $order) {
	if ($template_type == 'invoice') { // put due date only on invoice
		$invoice_date = method_exists($order, 'get_meta') ? $order->get_meta('_wcpdf_invoice_date',true,'edit') : get_post_meta( $order->id, '_wcpdf_invoice_date', true );
		$due_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date . ' + 14 days') );
		?>
		<tr class="due-date">
			<th>Due Date:</th>
			<td><?php echo $due_date; ?></td>
		</tr>
		<?php
	}
}

You may want to add some custom styles to make this look a bit better.

add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_payment_instructions', 10, 2 );
function wpo_wcpdf_payment_instructions ($template_type, $order) {
    if ($template_type == 'proforma') { // change 'proforma' to 'invoice' if you want to put this on the invoice
        WC()->payment_gateways();
        $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id;
        $payment_method = method_exists($order, 'get_payment_method') ? $order->get_payment_method() : $order->payment_method;
        do_action( 'woocommerce_thankyou_' . $payment_method, $order_id );
    }
}