• Resolved c8000

    (@c8000)


    Hi,
    I want to return a custom field in my packing slip only if the field has a value. The following code is not working though? What am I doing wrong?

    
    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_delivery_date', 10, 2 );
    function wpo_wcpdf_delivery_date ($template_type, $order) {
        if ($template_type == 'packing-slip') {
            $document = wcpdf_get_document( $template_type, $order );
            if (!empty($document->custom_field('Delivery Time'))) {
            ?>
            <tr class="delivery-date">
                <th><?php echo __('Delivery Time','domain').": "; ?></th>
                <td><?php $document->custom_field('Delivery Time'); ?></td>
            </tr>
            <?php
        	}
        }
    }
    

    Thank you very much.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Hi! The code is not working because 1) you can’t do empty on a function return value and 2) the function you’re using is a display function that doesn’t return anything but echos the value directly. You can use the get_custom_field() method instead:

    
    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_delivery_date', 10, 2 );
    function wpo_wcpdf_delivery_date ($template_type, $order) {
    	if ($template_type == 'packing-slip') {
    		$document = wcpdf_get_document( $template_type, $order );
    		$delivery_time = $document->get_custom_field('Delivery Time');
    		if (!empty($delivery_time)) {
    		?>
    		<tr class="delivery-date">
    			<th><?php echo __('Delivery Time','domain').": "; ?></th>
    			<td><?php echo $delivery_time; ?></td>
    		</tr>
    		<?php
    		}
    	}
    }
    
    Thread Starter c8000

    (@c8000)

    Thank you so much!

    Plugin Contributor Ewout

    (@pomegranate)

    You’re welcome ??

    If you can spare a minute, I’d really appreciate a plugin review; https://www.ads-software.com/support/plugin/woocommerce-pdf-invoices-packing-slips/reviews/#new-post

    Thanks in advance and have a fantastic rest of the day!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Conditional Custom Field’ is closed to new replies.