• I love your plug in. I’m adding a custom field to the PDFs generated but I would like to query the shipping method and only show the field if the shipping is not pickup. This doesn’t seem to work, where am I going wrong? I’ve tried outputting the variable $shipping_method to see what it contains and it appears to be empty.

    add_action( ‘wpo_wcpdf_before_order_details’, ‘wpo_wcpdf_location’, 10, 2 );
    function wpo_wcpdf_location ($template_type, $order) {
    $document = wcpdf_get_document( $template_type, $order );
    $shipping_method = $order->get_shipping_method();
    if ($shipping_method !== ‘local_pickup’) {
    ?>
    <b>Delivery Location:</b> <?php $document->custom_field(‘Delivery Location’); ?><br><br>
    <?php
    }
    }`

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Darren Peyou

    (@dpeyou)

    Hi @cenders,

    I believe this is what you’re looking for:

    add_action('wpo_wcpdf_after_order_data', 'wpo_wcpdf_show_shipping_method', 10, 2); 
    function wpo_wcpdf_show_shipping_method ( $template_type, $order ) {
    	// if order is empty, bail!
    	if ( empty($order) ) {return;}
    	
    	foreach ( $order->get_shipping_methods() as $shipping_method ) {
    		$shipping_method_name = $shipping_method->get_name();
    		if ( strtolower($shipping_method_name) !== 'local pickup' ) {
    			?>
    			<tr class="shipping-method">
    				<th>Shipping Method: </th>
    				<td><?php echo $shipping_method_name; ?></td>
    			</tr>
    			<?php
    		}
    	}
    }
    Thread Starter Corbett Enders

    (@cenders)

    Thank you! That works.

    Actual code I used is:

    add_action('wpo_wcpdf_after_order_data', 'wpo_wcpdf_show_shipping_method', 10, 2); 
    function wpo_wcpdf_show_shipping_method ( $template_type, $order ) {
    	// if order is empty, bail!
    	if ( empty($order) ) {return;}
    	$document = wcpdf_get_document( $template_type, $order );
    	foreach ( $order->get_shipping_methods() as $shipping_method ) {
    		$shipping_method_name = $shipping_method->get_name();
    		if ( strtolower($shipping_method_name) === 'delivery' ) {
    			?><tr><td colspan="2"><br><b><?php $document->custom_field('Delivery Location'); ?></b></td></tr>
    			<?php
    		}
    	}
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Query shipping method’ is closed to new replies.