Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Cannot get custom field to display in invoice template

  • Resolved Jon Fergus

    (@jon-fergus)


    I’ve added a custom field using this method. Using the WooCommerce Store Toolkit plugin, I can see my custom field under the Product Post Meta. There it shows “_hscode” along with the value I entered for the product.

    I’ve created a custom invoice template, and tried adding the code as you recommend, so I have:

    <?php $this->custom_field('_hscode'); ?>

    Yet still nothing shows up on my invoice. I’m not well versed in php though, so maybe I’m making a simple error somewhere. Here’s a fuller picture of what I’ve done in my custom template:

    				<span class="item-meta"><?php echo $item['meta']; ?></span>
    				<dl class="meta">
    					<?php $description_label = __( 'SKU', 'woocommerce-pdf-invoices-packing-slips' ); // registering alternate label translation ?>
    					<?php if( !empty( $item['sku'] ) ) : ?><dt class="sku"><?php _e( 'SKU:', 'woocommerce-pdf-invoices-packing-slips' ); ?></dt><dd class="sku"><?php echo $item['sku']; ?></dd><?php endif; ?>
    			        <?php if( !empty( $item['weight'] ) ) : ?><dt class="weight"><?php _e( 'Weight:', 'woocommerce-pdf-invoices-packing-slips' ); ?></dt><dd class="weight"><?php echo $item['weight']; ?><?php echo get_option('woocommerce_weight_unit'); ?><?php endif; ?>.&nbsp;&nbsp;&nbsp;HS Code:&nbsp;<?php $this->custom_field('_hscode'); ?></dd>
    				</dl>
    				<?php do_action( 'wpo_wcpdf_after_item_meta', $this->type, $item, $this->order  ); ?>

    We want the HS Code (_hscode) to show up on the same line as the Weight, under each product.

    Any help would be much appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Ewout

    (@pomegranate)

    Hello Jon,
    The custom_field method is calling custom fields from the order, you need different functions to call custom fields from a product. This should work (assuming WC3.X):

    
    <?php if ( !empty($item['product'] ) ) echo $item['product']->get_meta('_hs_code'); ?>
    

    More information in the documentation:
    https://docs.wpovernight.com/woocommerce-pdf-invoices-packing-slips/displaying-product-custom-fields/
    You will probably want to look at the <dt> and <dd> too, because right now if there is no product weight the HTML will be broken (with only a closing </dd> and no opening tag) because the if/endif is wrapping only the weight bit.

    Hope that helps!
    Ewout

    Jon Fergus

    (@jon-fergus)

    I have no idea why, but I can’t get this to work either. Here’s my most recent attempt, and the result in the pdf is “HS Code: ” with no value:

    	<tbody>
    		<?php $items = $this->get_order_items(); if( sizeof( $items ) > 0 ) : foreach( $items as $item_id => $item ) : ?>
    		<tr class="<?php echo apply_filters( 'wpo_wcpdf_item_row_class', $item_id, $this->type, $this->order, $item_id ); ?>">
    			<td class="product">
    				<?php $description_label = __( 'Description', 'woocommerce-pdf-invoices-packing-slips' ); // registering alternate label translation ?>
    				<span class="item-name"><?php echo $item['name']; ?></span>
    				<?php do_action( 'wpo_wcpdf_before_item_meta', $this->type, $item, $this->order  ); ?>
    				<span class="item-meta"><?php echo $item['meta']; ?></span>
    				<dl class="meta">
    					<?php $description_label = __( 'SKU', 'woocommerce-pdf-invoices-packing-slips' ); // registering alternate label translation ?>
    					<?php if( !empty( $item['sku'] ) ) : ?><dt class="sku"><?php _e( 'SKU:', 'woocommerce-pdf-invoices-packing-slips' ); ?></dt><dd class="sku"><?php echo $item['sku']; ?></dd><?php endif; ?>
    			        <?php if( !empty( $item['weight'] ) ) : ?><dt class="weight"><?php _e( 'Weight:', 'woocommerce-pdf-invoices-packing-slips' ); ?></dt><dd class="weight"><?php echo $item['weight']; ?><?php echo get_option('woocommerce_weight_unit'); ?></dd><?php endif; ?>
                        <dt>HS Code:&nbsp;</dt><dd><?php if ( !empty($item['product'] ) ) echo $item['product']->get_meta('_hscode'); ?></dd>
        			</dl>
    				<?php do_action( 'wpo_wcpdf_after_item_meta', $this->type, $item, $this->order  ); ?>
    			</td>
    			<td class="quantity"><?php echo $item['quantity']; ?></td>
    			<td class="price"><?php echo $item['order_price']; ?></td>
    		</tr>
    		<?php endforeach; endif; ?>
    	</tbody>
    Plugin Author Ewout

    (@pomegranate)

    is this a variable product or a simple product? If it’s a variable product, the query looks a bit different.

    Jon Fergus

    (@jon-fergus)

    Ah, yes, it is a variable product. The hscode is set at the product level like sku (not for each variable) if that makes a difference.

    • This reply was modified 3 months ago by  Jon Fergus.
    • This reply was modified 3 months ago by  Jon Fergus.
    Jon Fergus

    (@jon-fergus)

    Found the solution in another of your replies, adding ->parent to the code, and it works well. Thanks for the help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.