• 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 to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    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 5 years, 4 months ago by Ewout. Reason: only print on invoice
Viewing 1 replies (of 1 total)
  • The topic ‘If statement custom field’ is closed to new replies.