Pdf Invoice & Packing slips: Show the deepest category next to product name.

Add this action to your WordPress function to display the deepest product category next to product name.

example:

Product Name: Women Red Jacket
Categories: Women > Jackets > Red Jackets

on invoice, it will show, for each product line:

Women Red Jacket – Red Jackets

add_action( 'wpo_wcpdf_after_item_meta', 'giodc_show_deepest_category_pdf_invoice', 10, 3 );
function giodc_show_deepest_category_pdf_invoice ( $template_type, $item, $order ) {
 
    if (isset($item['product'])) {

	// get all product cats for the current post
	$categories = get_the_terms( $item['product']->get_id(), 'product_cat' ); 

	// wrapper to hide any errors from top level categories or products without category
	if ( $categories && ! is_wp_error( $category ) ) : 

    // loop through each cat
    foreach($categories as $category) :
      // get the children (if any) of the current cat
      $children = get_categories( array ('taxonomy' => 'product_cat', 'parent' => $category->term_id ));

      if ( count($children) == 0 ) {
          // if no children, then echo the category name.
		  echo '<div class="product-categories"> - ' . $category->name . '</div>';

      }
    endforeach;
endif;

    }
}

Useful if for example, your product name is a Code (like ABC123) and you want to add more information on customer invoice.

Leave a Reply

avatar
  Subscribe  
Notify of