Title: If statement custom field
Last modified: July 4, 2019

---

# If statement custom field

 *  [jaribvdo](https://wordpress.org/support/users/jaribvdo/)
 * (@jaribvdo)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/if-statement-custom-field/)
 * Hi,
 * I have created 2 custom fields which are included in the invoice. But the fields
   are not required so when a customer does not fill in the field it should not 
   be visible on the invoice but it is.
 * Here is the code.
 *     ```
       add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_delivery_date', 10, 2 );
       function wpo_wcpdf_delivery_date ($template_type, $order) {
           $document = wcpdf_get_document( $template_type, $order );
           if ($template_type == 'invoice') {
               ?>
               <tr class="delivery-date">
                   <th>KvK nummer</th>
                   <td><?php $document->custom_field('billing_kvk'); ?></td>
               </tr>
               <tr class="delivery-date">
                   <th>BTW nummer</th>
                   <td><?php $document->custom_field('billing_btw'); ?></td>
               </tr>
               <tr class="delivery-date">
                   <th>Inkooporder</th>
                   <td><?php $document->custom_field('billing_inkooporder'); ?></td>
               </tr>
               <?php
           }
       ```
   
 * How can I make a if statement like when field `billing_btw` is empty dont show
   the `<tr class="delivery-date">`?
 * Thanks for your time!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fif-statement-custom-field%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Plugin Contributor [Ewout](https://wordpress.org/support/users/pomegranate/)
 * (@pomegranate)
 * [6 years, 11 months ago](https://wordpress.org/support/topic/if-statement-custom-field/#post-11700207)
 * For if statements you could directly query the order meta. Here’s a somewhat 
   more advanced example:
 *     ```
       add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_print_custom_fields', 10, 2 );
       function wpo_wcpdf_print_custom_fields ( $template_type, $order ) {
           if ( $template_type != 'invoice' ) {
               return;
           }
           $fields = array(
               'billing_kvk'         => 'KvK nummer',
               'billing_btw'         => 'BTW nummer',
               'billing_inkooporder' => 'Inkooporder',
           );
   
           foreach ( $fields as $fieldname => $title ) {
               if ( $value = $order->get_meta( $fieldname ) ) {
                   ?>
                   <tr class="<?php echo esc_attr( $fieldname ); ?>">
                       <th><?php echo $title; ?>:</th>
                       <td><?php echo $value; ?></td>
                   </tr>
                   <?php
               }
           }
       }
       ```
   
 * Note that I have copied your exact field names, but the official meta getter 
   doesn’t have a fallback to underscore prefixed (hidden) custom fields, so you
   may need to add an underscore if it doesn’t work (i.e. `_billing_kvk` instead
   of `billing_kvk`)
 * Hope that helps!
    -  This reply was modified 6 years, 11 months ago by [Ewout](https://wordpress.org/support/users/pomegranate/).
      Reason: only print on invoice

Viewing 1 replies (of 1 total)

The topic ‘If statement custom field’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce-pdf-invoices-packing-slips/assets/icon-256x256.
   png?rev=2189942)
 * [PDF Invoices & Packing Slips for WooCommerce](https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/reviews/)

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)
 * [if](https://wordpress.org/support/topic-tag/if/)

 * 1 reply
 * 2 participants
 * Last reply from: [Ewout](https://wordpress.org/support/users/pomegranate/)
 * Last activity: [6 years, 11 months ago](https://wordpress.org/support/topic/if-statement-custom-field/#post-11700207)
 * Status: not resolved