Displaying product custom fields

 

As with all post types in WordPress, you can add custom fields (post meta) to your WooCommerce products:

WooCommerce Product Custom Field

 

Theres two ways to get these to display in your PDF invoice or packing slip: with a pdf template action hook in your themes functions.php (which is the most update safe method because you don’t have to create a custom template), or directly in a custom template.

If you’ve never edited your themes functions.php before, make sure to read this first.

Let’s say your custom field is called ‘Location’, you can display this below the product description with the following code:

add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_product_custom_field', 10, 3 );
function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) {
    // check if product exists first
    if (empty($item['product'])) return;

    // replace 'Location' with your custom field name!
    $location = get_post_meta( $item['product']->id, 'Location', true );
    if (!empty($location)) {
        echo '<div class="product-location">Location: '.$location.'</div>';
    }
}

If you want to restrict this to the Packing Slip, you can add an if-statement around it:

add_action( 'wpo_wcpdf_after_item_meta', 'wpo_wcpdf_product_custom_field', 10, 3 );
function wpo_wcpdf_product_custom_field ( $template_type, $item, $order ) {
    if ( $template_type == 'packing-slip' ) {
        // check if product exists first
        if (empty($item['product'])) return;

        // replace 'Location' with your custom field name!
        $location = get_post_meta( $item['product']->id, 'Location', true );
        if (!empty($location)) {
            echo '<div class="product-location">Location: '.$location.'</div>';
        }
    }
}

If you’re making more than just this modification in the template, you may find it easier /more convenient to create a custom template and insert the code directly into it. Here’s what you can use in that case (replace ‘Location’ with your custom field name):

<?php 
if ( !empty($item['product']) && $location = get_post_meta( $item['product']->id, 'Location', true ) ) {
    echo '<div class="product-location">Location: '.$location.'</div>';
}
?>

You can only insert this code into the items table (or loop through the products with a separate foreach loop).