• Resolved ChrisL

    (@chrslcy)


    I’m currently using this code to display a custom field (‘purchase_order’) on the invoice and packing slip.

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_purchase_order', 10, 2 );
    
    function wpo_wcpdf_purchase_order ($document_type, $order) { 
    $document = wcpdf_get_document( $document_type, $order ); ?>
            <tr class="purchase-order">
                <th>Purchase Order:</th>
                <td><?php $document->custom_field('purchase_order'); ?></td>
            </tr>
        <?php
    }

    How can I ensure that the title ‘Purchase Order:’ does not display when the custom field is either non existent or empty?

    My attempts at wrapping the code in a conditional are not working.

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_purchase_order', 10, 2 );
    
    function wpo_wcpdf_purchase_order ($document_type, $order) {
    $document = wcpdf_get_document( $document_type, $order );
    $purchase_order = $document->custom_field('purchase_order');
    
    if (!empty($purchase_order)) {
        ?>
        <tr class="purchase-order">
            <th>Purchase Order:</th>
            <td><?php echo $purchase_order; ?></td>
        </tr>
        <?php
    }

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor dwpriv

    (@dwpriv)

    Check to see that your purchase_order meta key is actually empty in an order that isn’t a purchase order. It’s possible the meta key is there but has a value of ‘no’ or similar.

    Thread Starter ChrisL

    (@chrslcy)

    Thanks for the prompt response.

    When I use the first example code, the invoice always displays the title ‘Purchase Order:’ and only displays the value of the purchase_order custom field when such a field exists and has a value.

    However, when I use the second example code, nothing is displayed, whether a purchase_order custom field is present and populated or not.

    Any idea what I’m doing wrong?

    • This reply was modified 8 months, 2 weeks ago by ChrisL.
    Plugin Contributor dwpriv

    (@dwpriv)

    Try this snippet

    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_purchase_order', 10, 2 );
    function wpo_wcpdf_purchase_order ($document_type, $order) {
    $document = wcpdf_get_document( $document_type, $order );
    $purchase_order = $order->get_meta('purchase_order');
    
    	if ( ! empty($purchase_order ) ) {
    		?>
    		<tr class="purchase-order">
    			<th>Purchase Order:</th>
    			<td><?php echo $purchase_order; ?></td>
    		</tr>
    		<?php
    	}
    }
    Thread Starter ChrisL

    (@chrslcy)

    Thank you. That worked. Much appreciated!

    Plugin Contributor dwpriv

    (@dwpriv)

    No problem ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hide empty custom field from Invoice’ is closed to new replies.