Editing Template(s) of Yith PDF Invoice (WooCommerce Extension)

Published Apr 28, 2017Last updated Aug 08, 2017
Editing Template(s) of Yith PDF Invoice (WooCommerce Extension)

I haven't posted before, but the Write Post button always comes up after a session and this time I thought, why not post the details of what I worked on. Today I was modifying a Yith Pdf Invoice template to add shipping details and the order note entered on the checkout page to the template. Here is the code I ended with - which is in a child theme template override:

<?php

if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
?>
<div class="ywpi-customer-details">

<div class="ywpi-customer-content">
<em>Invoice to:</em><br/>
<?php echo $content; ?>
</div>

<?php 
$shipping_address = wp_kses( YITH_PDF_Invoice()->get_customer_shipping_details( $document->order->id ), array( "br" => array() ) );
if (!empty($shipping_address) && $shipping_address != $content) { ?>
<div class="ywpi-customer-content">
<br/><em>Shipping to:</em><br/>
<?php echo $shipping_address; ?>
</div><?php } ?>

<div class="delivery-instructions">
<em>Special instruction for this delivery:</em><br/>
<?php 
$note = $document->order->post->post_excerpt;
if (!empty($note)) {
?><?php echo $note; ?><?php
} ?>
</div>
</div>

The template overrided the yith-woocommerce-pdf-invoice-premium/teplates/yith-pdf-invoice/customer-details.php template which starts out with:

<?php
if ( ! defined( 'ABSPATH' ) ) {
  exit;
} // Exit if accessed directly
?>
<div class="ywpi-customer-details">

    <div class="ywpi-customer-content">
      <?php echo $content; ?>
    </div>
</div>

So you can see where I have added the section to add shipping address if different from billing address in an additional ywpi-customer-content block:

<?php 
$shipping_address = wp_kses( YITH_PDF_Invoice()->get_customer_shipping_details( $document->order->id ), array( "br" => array() ) );
if (!empty($shipping_address) && $shipping_address != $content) { ?>
<div class="ywpi-customer-content">
<br/><em>Shipping to:</em><br/>
<?php echo $shipping_address; ?>
</div><?php } ?>

And the customer note (stored as post_excerpt) to a new html block below:

<div class="delivery-instructions">
<em>Special instruction for this delivery:</em><br/>
<?php 
$note = $document->order->post->post_excerpt;
if (!empty($note)) {
?><?php echo $note; ?><?php
} ?>
</div>

And that had the desired effect. The most important thing is to know that the $document variable accessible from the templates will be the main Yith class for the type of document (YITH_Document or a child class) in most of these if not all the WC_Order is stored as a property ->order, which gives access to any data you may need. It is possible a filter or action hook could be found to more easily add the custom data, but as the project was already overriding templates this was the best approach.

For more articles like this please visit my site - webbyscots.com.

Discover and read more posts from Liam Bailey
get started