How to Add Shipping Tracking in WooCommerce (Custom Code)
Adding shipping tracking to your WooCommerce store helps improve customer satisfaction by keeping customers informed about their orders. In this guide, we’ll walk you through how to manually add shipping tracking functionality using custom code, with a quick alternative using plugins if coding isn’t your preference.
Step 1: Add Tracking Number Field to WooCommerce Orders
First, let’s add a simple field to include tracking numbers on the WooCommerce order editing screen:
add_action('woocommerce_admin_order_data_after_shipping_address', 'wpo_add_custom_tracking_field');
function wpo_add_custom_tracking_field($order){
woocommerce_wp_text_input([
'id' => 'tracking_number',
'label' => 'Tracking Number:',
'value' => get_post_meta($order->get_id(), 'tracking_number', true)
]);
}
Note: This snippet adds the tracking number field to the backend, but you also need to save this data to the order’s metadata for it to be usable. To do this, you’ll need to add another snippet to your site, which will save the entered tracking numbers to the order meta (Step 2)
Step 2: Save Your Tracking Numbers
To make the tracking number added by the store owner available for display, you need to save it in the order meta data. After adding this snippet, you can enter the tracking number on the order editing page in the custom field we created (in step 1) and click ‘Update’ to save the changes. Once saved, the tracking number becomes available for display on the customer’s account page and order emails.
add_action('woocommerce_process_shop_order_meta', 'wpo_save_tracking_custom_field');
function wpo_save_tracking_custom_field($order_id){
update_post_meta($order_id, 'tracking_number', sanitize_text_field($_POST['tracking_number']));
}
Step 3: Display Tracking on Customer Account Pages
To display tracking numbers to customers on their order details page, use the following code snippet:
add_action('woocommerce_order_details_after_order_table', 'wpo_display_tracking_number_customer');
function wpo_display_tracking_number_customer($order) {
$tracking_number = get_post_meta($order->get_id(), 'tracking_number', true);
if (!empty($tracking_number)) {
echo '<p><strong>Tracking Number:</strong> ' . esc_html($tracking_number) . '</p>';
}
}
Step 4: Include Tracking in Order Confirmation Emails
Ensure your customers receive their tracking numbers via confirmation emails:
add_action('woocommerce_email_after_order_table', 'wpo_email_custom_tracking', 10, 4);
function wpo_email_custom_tracking($order, $sent_to_admin, $plain_text, $email) {
$tracking_number = get_post_meta($order->get_id(), 'tracking_number', true);
if (!empty($tracking_number)) {
echo '<p>Your tracking number: ' . esc_html($tracking_number) . '</p>';
}
}
PRO TIP: Always test custom snippets on a staging site first! Need guidance? Check our tutorial on how to add custom code to your WooCommerce website.
Prefer a Plugin? Here are Alternatives
If coding isn’t your thing, consider these user-friendly plugins:
Installation:
- Navigate to
WordPress Dashboard
>Plugins
>Add New
. - Search, install, and activate your preferred plugin.
- Configure it under
WooCommerce
>Settings
>Shipping
orWooCommerce Shipment Tracking Pro
>Settings
.
Optional Integration: PDF Invoices
Streamline your workflow further by adding tracking numbers to your invoices and packing slips with our WooCommerce PDF Invoices & Packing Slips plugin.
Step 4: Monitor Shipping Effectively
Regularly review tracking statuses on your WooCommerce orders dashboard to proactively manage shipments and enhance customer satisfaction.
PRO TIP: Regular checks help you quickly address issues and ensure smooth delivery experiences.
Conclusion
Whether you prefer coding or plugins, adding shipping tracking in WooCommerce enhances customer experience and simplifies your store management.
Check out our other plugins and blog posts to further enhance your WooCommerce store!