Include tracking provider and tracking number in Packing Slip
-
Hello and thanks for the great plug-in. I would like to add the tracking provider and tracking number to a custom Packing Slip template, in the Order Data area. I am using the Woocommerce Shipment Tracking plug-in:
https://woocommerce.com/products/shipment-tracking/
What is the best way to do that?
-
Hello @pr-sib
The best way is with our Premium Templates and his customizer, because you can add custom meta easily with custom blocks.
You can achieve the same result with action hooks, but requires some additional code.
Thanks. So I have used Example 1 to print a delivery date on the packing slip in template-functions.php, which puts the custom field in the correct place.
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 == 'packing-slip') { ?> <tr class="delivery-date"> <th>Delivery Date:</th> <td><?php $document->custom_field('delivery_date'); ?></td> </tr> <?php } }
I guess I just don’t know exactly how I should modify this code to use the meta references provided here, so I can replace the delivery date with the tracking provider and tracking number. I know I need to use
tracking_provider
andtracking_number
somehow, I just don’t know what the code needs to look like.Hello @pr-sib
Try this and let me know if it works:
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_shipping_tracking', 10, 2 ); function wpo_wcpdf_shipping_tracking($template_type, $order) { if ($template_type == 'invoice') { $document = wcpdf_get_document( $template_type, $order ); ?> <tr class="tracking"> <th>Tracking:</th> <td><?php $document->custom_field('tracking_provider'); ?></td> <td><?php $document->custom_field('tracking_number'); ?></td> </tr> <?php } }
Hi @alexmigf and thanks for your reply.
Almost there. However, no value is shown for
tracking_provider
andtracking_number
. I followed your advice and installed the Woocommerce Store Toolkit plug-in and I found this in the Order Post Meta:_wc_shipment_tracking_items ? 0 ? ? tracking_provider fedex ? ? custom_tracking_provider ? ? custom_tracking_link ? ? tracking_number 770188300645 ? ? date_shipped 1586217600 ? ? tracking_id 45a3b570e48da984f386425dfb69a296
So I guess I just need to know how to access those items since they are not “first-level” (sorry I am not a developer so I don’t know the correct term).
Hi @pr-sib
It’s an array of data, you should iterate like this:
$tracking_items = get_post_meta( $order_id, '_wc_shipment_tracking_items', true ); foreach ( $tracking_items as $tracking_item ){ echo esc_html( $tracking_item['tracking_provider'] ); echo "<br>"; echo esc_html( $tracking_item['tracking_number'] ); }
Thanks, @alexmigf. Forgive me, I don’t understand if I should be replacing the earlier code that you sent me, or adding this new code somewhere.
Hi @pr-sib
Try this:
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_shipping_tracking', 10, 2 ); function wpo_wcpdf_shipping_tracking($template_type, $order) { if ($template_type == 'invoice') { $order_id = $order->get_id(); $tracking_items = get_post_meta( $order_id, '_wc_shipment_tracking_items', true ); echo '<tr class="tracking">'; echo '<th>Tracking:</th>'; foreach( $tracking_items as $tracking_item ) { echo '<td>'.esc_html( $tracking_item['tracking_provider'] ).'</td>'; echo '<td>'.esc_html( $tracking_item['tracking_number'] ).'</td>'; } echo '</tr>'; } }
You have done it! Thank you so much for your patience and help with this.
You’re welcome ??
P.S. In case anyone else needs this, I replaced
invoice
in your code withpacking-slip
, since that really where we use it. Our workflow is, once the tracking number is generated with FedEx, etc., we print the label, update the order in WP and then print the packing slip, to match it to the label and ensure the contents are correct. Thanks again.This is the exact solution that I a looking for but not able to implement it. Can someone please guide me on where should I add the code ? Can provide details if requested.
@majithiaharshil I put in in the template-functions.php file, which goes in this folder: wp-content > themes > [your child theme] > woocommerce > pdf > [whatever you name your custom setting]
Just a small improvement to the above action hook, which makes sure it has better compatibility with future versions of WooCommerce:
add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_shipping_tracking', 10, 2 ); function wpo_wcpdf_shipping_tracking($template_type, $order) { if ($template_type == 'packing-slip') { if ( $tracking_items = $order->get_meta('_wc_shipment_tracking_items') ) { ?> <tr class="tracking"> <th>Tracking:</th> <td> <?php foreach( $tracking_items as $tracking_item ) { echo esc_html( $tracking_item['tracking_provider'] )."<br>"; echo esc_html( $tracking_item['tracking_number'] ); } ?> </td> </tr> <?php } } }
(where you can replace
packing-slip
withinvoice
if you want to limit this to invoices)@majithiaharshil If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters
Thank you. I will give it a try.
I have 1 more question.
Will this code directly create a table after order details or will I get the fields tracking_number and tracking_provider which I can use it in plugin?<table style="border: 1px solid black;"> <tr> <td style="border: 1px solid black;"> PO Number</td> <td style="border: 1px solid black;"> Terms </td> <td style="border: 1px solid black;"> Via </td> <td style="border: 1px solid black;"> Tracking </td> </tr> <tr> <td style="border: 1px solid black;"> {{_purchase_order_number}}</td> <td style="border: 1px solid black;"> </td> <td style="border: 1px solid black;"> {{tracking_number}} </td> <td style="border: 1px solid black;"> {{tracking_provider}} </td> </tr> </table>
I want to bring the fields in the Content of PDF Invoice.
- This reply was modified 4 years, 6 months ago by majithiaharshil.
Thanks for the update, @pomegranate ! I just swapped it in and it looks good. And I see you already updated your post and replaced “invoice” with “packing-slip”) – thanks!
@majithiaharshil On your invoice (or packing slip in my case), it adds the tracking number right after the tracking method (i.e., FedEx, UPS, etc), in the “Tracking:” section.
- The topic ‘Include tracking provider and tracking number in Packing Slip’ is closed to new replies.