Support » Plugin: WooCommerce PDF Invoices & Packing Slips » Print shipping tracking on Invoice and order details

  • Hello,

    How can I Print shipping tracking number and shipping date on Invoice and order details?

    I am using a plugin that allows me to send shipping tracking number and shipping date email notification to customer. They can provide me the information to retrieve the custom order meta related to the tracking data. Can you advise How can I achieve the goal?

    Thanks in advance!

Viewing 15 replies - 1 through 15 (of 22 total)
  • Plugin Support Darren Peyou

    (@dpeyou)

    Hi @cfm168,

    It really depends on how this data is saved in WooCommerce.
    Displaying Custom Fields

    So if you know the fields (let’s assume for the sake of this conversation: custom_field_1 & custom_field_2), you could insert them after the order details area as follows:

    
    add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_tracking_and_date', 10, 2 );
    function wpo_wcpdf_tracking_and_date ($template_type, $order) {
        if ($template_type == 'invoice') {
            $document = wcpdf_get_document( $template_type, $order );
            ?>      
            <div class="shipping-tracking-date">
                <b>Tracking Number:</b>
                <div><?php $document->custom_field('custom_field_1'); ?></div>
                <b>Shipping Date:</b>
                <div><?php $document->custom_field('custom_field_2'); ?></div>
            </div>
            <?php
        }
    }
    
    Thread Starter cfm168

    (@cfm168)

    Hi @dpeyou,

    The shipping tracking plugin author provided me a long snippet.
    1) Should I put the snippet after the code you provided above?
    2) The author advised me: “where you have to replace the $order_id with the variable that holds the current order id. The $shipping_traking_num and the $dispatch_date will hold the data you need to print inside the invoice.”, How should I do?

    Please advise. Thanks in advance!

    Plugin Author Ewout

    (@pomegranate)

    @cfm168 if you share the snippet that was provided to you we can answer the questions in 2).

    Thread Starter cfm168

    (@cfm168)

    Sure, will send to you via email. I think I have your email. Thanks!

    Plugin Author Ewout

    (@pomegranate)

    If you can post it here, it’s even better, that way other people with the same issue will benefit from it too!

    Thread Starter cfm168

    (@cfm168)

    Here is the snippet:

    global $wcst_tracking_info_displayer, $wcst_time_model;

    $tracking_meta = $wcst_tracking_info_displayer->get_order_meta($order_id);
    $shipping_traking_num = $tracking_meta[‘_wcst_order_trackno’][0];

    $dispatch_date = isset($tracking_meta[‘_wcst_order_dispatch_date’][0]) ? $tracking_meta[‘_wcst_order_dispatch_date’][0] : __( ‘N/A’, ‘woocommerce’ ) ;
    $dispatch_date = $wcst_time_model->format_data($dispatch_date);

    (it should look like this: https://www.dropbox.com/s/83i17tlxwsxv38b/wcst_snippet.jpg?dl=0 )

    Thread Starter cfm168

    (@cfm168)

    Hi @pomegranate,
    Any updates for me? Thanks in advance!

    Plugin Author Ewout

    (@pomegranate)

    Thanks for the heads up! With a small modification you can use that code directly in the form that @dpeyou formatted it for you earlier. Can you try this?

    
    add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_tracking_and_date', 10, 2 );
    function wpo_wcpdf_tracking_and_date ($template_type, $order) {
    	if ($template_type == 'invoice') {
    		global $wcst_tracking_info_displayer, $wcst_time_model;
    
    		$tracking_meta = $wcst_tracking_info_displayer->get_order_meta( $order->get_id() );
    		$shipping_traking_num = $tracking_meta['_wcst_order_trackno'][0];
    
    		$dispatch_date = isset($tracking_meta['_wcst_order_dispatch_date'][0]) ? $tracking_meta['_wcst_order_dispatch_date'][0] : __( 'N/A', 'woocommerce' ) ;
    		$dispatch_date = $wcst_time_model->format_data($dispatch_date);
    		?>
    		<div class="shipping-tracking-date">
    			<b>Tracking Number:</b>
    			<div><?php echo $shipping_traking_num; ?></div>
    			<b>Shipping Date:</b>
    			<div><?php $dispatch_date; ?></div>
    		</div>
    		<?php
    	}
    }
    
    Thread Starter cfm168

    (@cfm168)

    Hi @pomegranate,

    Where should I put these code in? Should I put it in functions.php under my child theme?

    Plugin Support Yordan Soares

    (@yordansoares)

    Hello @cfm168,

    Please read this guide to learn how to work with code snippets (actions/filters) or functions.php: How to use filters

    Thread Starter cfm168

    (@cfm168)

    Hi @pomegranate,

    The code you provided are not working and get the following error when I try to download the invoice from admin:

    Fatal error: Call to undefined method WCST_Tracking_info_displayer::get_order_meta()

    I added the code on the child theme. It also causes the Invoice to become not downloadable but view only. Delete the code every thing back to good normal.

    I have sent the error details to your email. Please check and help.

    Thanks in advance!

    Plugin Author Ewout

    (@pomegranate)

    @cfm168 This part of the code is what was supplied by you in your previous post (here), we didn’t make any changes to this other than replacing $order_id by $order->get_id()

    Thread Starter cfm168

    (@cfm168)

    Hello Ewout,

    The author made a correction, and sent me new code as below (between the lines):
    —————————————————————————————
    global $wcst_order_model, $wcst_time_model;

    $tracking_meta = $wcst_order_model->get_order_meta($order_id);
    $shipping_traking_num = $tracking_meta[‘_wcst_order_trackno’][0];

    $dispatch_date = isset($tracking_meta[‘_wcst_order_dispatch_date’][0]) ? $tracking_meta[‘_wcst_order_dispatch_date’][0] : __( ‘N/A’, ‘woocommerce’ ) ;
    $dispatch_date = $wcst_time_model->format_data($dispatch_date);
    —————————————————————————————

    Please help and give me your code so I can put them on my child theme functions file. Thanks in advance!

    Plugin Support Darren Peyou

    (@dpeyou)

    Hey @cfm168,

    The code updated with what you provided:

    add_action( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_tracking_and_date', 10, 2 );
    function wpo_wcpdf_tracking_and_date ($template_type, $order) {
    	if ($template_type == 'invoice') {
    		global $wcst_order_model, $wcst_time_model;
    
                    $tracking_meta = $wcst_order_model->get_order_meta($order->get_id());
                    $shipping_traking_num = $tracking_meta[‘_wcst_order_trackno’][0];
    
                    $dispatch_date = isset($tracking_meta[‘_wcst_order_dispatch_date’][0]) ? $tracking_meta[‘_wcst_order_dispatch_date’][0] : __( ‘N/A’, ‘woocommerce’ ) ;
                    $dispatch_date = $wcst_time_model->format_data($dispatch_date);
    		?>
    		<div class="shipping-tracking-date">
    			<b>Tracking Number:</b>
    			<div><?php echo $shipping_traking_num; ?></div>
    			<b>Shipping Date:</b>
    			<div><?php $dispatch_date; ?></div>
    		</div>
    		<?php
    	}
    }
    Thread Starter cfm168

    (@cfm168)

    Hi @dpeyou,

    Thank you for your quick response. Now I can see the following printed on all previous Invoices.
    Tracking Number:
    Shipping Date:

    But the value does not show up. Is it because of previous invoices? Will works for new invoice?

    Please advise. Thanks!

Viewing 15 replies - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.