Codementor Events

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
post commentsBe the first to share your opinion
Alen
3 years ago

Dear Liam,

I have installed the Local delivery driver plugin, which i can assign the order to driver_ how i can add the driver name in YITH PDF invoice

The below PHP code i think will help you related to Driver plugin _

<?php
/**

/**

  • Fired during plugin activation.

  • This class defines all code necessary to run during the plugin’s activation.

  • @since 1.0.0

  • @package LDDFW

  • @subpackage LDDFW/includes

  • @author powerfulwp cs@powerfulwp.com
    /
    class LDDFW_Driver {
    /
    *

    • Drivers query
    • @since 1.0.0
    • @return array
      */
      public static function lddfw_get_drivers() {
      $args = array(
      ‘role’ => ‘driver’,
      ‘meta_query’ => array(
      ‘relation’ => ‘OR’,
      array(
      ‘key’ => ‘lddfw_driver_availability’,
      ‘compare’ => ‘NOT EXISTS’,
      ‘value’ => ‘’,
      ),
      array(
      ‘key’ => ‘lddfw_driver_availability’,
      ‘compare’ => ‘EXISTS’,
      ),
      ),
      ‘orderby’ => ‘meta_value ASC,display_name ASC’,
      ‘posts_per_page’ => -1,
      );
      return get_users( $args );
      }

    /**

    • Assign delivery order

    • @param int $order_id The order ID.

    • @param int $driver_id The driver ID.

    • @param string $operator The type.

    • @return void
      */
      public static function assign_delivery_driver( $order_id, $driver_id, $operator ) {

      $order = new WC_Order( $order_id );
      $order_driverid = get_post_meta( $order_id, ‘lddfw_driverid’, true );

      if ( $driver_id !== $order_driverid && ‘-1’ !== $driver_id && ‘’ !== $driver_id ) {

       $driver      = get_userdata( $driver_id );
       $driver_name = $driver->display_name;
       $note = __( 'Delivery driver has been assigned to order', 'lddfw' );
      
       $user_note = '';
       if ( lddfw_fs()->is__premium_only() ) {
       	if ( lddfw_fs()->is_plan( 'premium', true ) ) {
       		$current_user = wp_get_current_user();
       		if ( $current_user->exists() ) {
       			/* translators: %s: driver name */
       			$user_note = sprintf( ' ' . __( 'by %s', 'lddfw' ), $current_user->display_name );
       		}
       		/* translators: %s: driver name */
       		$note = sprintf( __( 'Delivery driver %s has been assigned to order%s', 'lddfw' ), $driver_name, $user_note );
       	}
       }
      
       update_post_meta( $order_id, 'lddfw_driverid', $driver_id );
Show more replies