How to Setup Shipment Tracking in WooCommerce?

Ecommerce, WooCommerce
How to Setup Shipment Tracking in WooCommerce?

Do your customers ask to see how they can track their order? Need a way to easily add tracking numbers to orders in your WooCommerce store?

In this guide, we are going to show you how to setup the WooCommerce Shipment Tracking plugin on your WooCommerce store.

The WooCommerce Shipment Tracking plugin is a easy way to allow customers to track their shipment. Whether you are manually adding the tracking number or using 3rd party tools like ShippingEasy or ShipStation, this plugin is for you.

The cost is $49/year but supports a lot of shipping carriers and countries (i.e. USPS, UPS, Fedex, and DHL to name a few).

See below for a quick video below that goes over the installation process:

How to setup Shipment Tracking in Woo?

The first step is to purchase this plugin on the WooCommerce marketplace.

Next, download the .zip file and navigate to your WordPress dashboard.

Then, go to Plugins, add a new plugin, and upload the .zip file.

Finally, activate the plugin. If you navigate to the WooCommerce Orders page in your WooCommerce backend, you will see that the Shipment Tracking column has been added.

How to add a tracking number to a order?

On your WooCommerce Orders page, click into a recent order of yours.

Next, in the top righthand corner, click “Add Tracking Number”.

Then, add the Provider and Tracking Number, then click Save Tracking.

Finally, if you want to notify your customer that their order has been shipped and provide the tracking link in their email confirmation, update the orders status from processing to completed.

How can you automate this process further?

If you use a plugin such as ShippingEasy or ShipStation to print off your labels, these shipping tools will automatically POST back to your WooCommerce store with the tracking number.

Here is how the process works:

  1. Customer orders a product on your store
  2. Orders that are “processing” will be exported to ShipStation or ShippingEasy
  3. Store manager prints off label and ships product to customer
  4. ShippingEasy or ShipStation POST back to your WooCommerce store and updates order status to “completed”
  5. Automated email goes out to customer that their shipment is on their way with tracking number available on the automated email

How to add the “Track Shipment” link to My Account – Orders page?

Edit your current orders.php file and search for the following snippet on lines 56 and 57:

// Search for this line of code in your orders.php file
<?php elseif ( 'order-status' === $column_id ) : ?>
	<?php echo esc_html( wc_get_order_status_name( $order->get_status() ) ); ?>

Replace with this snippet below:

// replace with the following code below in your orders.php file
<?php elseif ('order-status' === $column_id) :

	$status = $order->get_status();
	$column_output = "";

	// show tracking link
	if ($status == 'completed' && class_exists('WC_Shipment_Tracking_Actions')) {

		$st_actions = WC_Shipment_Tracking_Actions::get_instance();

		if ($tracking_items = $st_actions->get_tracking_items($order->get_id(), true)) {

			foreach ($tracking_items as $tracking_item) {

				if ($tracking_item['formatted_tracking_link'] !== '') {

					$column_output = sprintf("<a href='%s' target='_blank'>%s</a>",
						$tracking_item['formatted_tracking_link'], __('Track Shipment', 'bdl'));

					break;
				}
			}
		}
	}

	if (empty($column_output))
		$column_output = esc_html(wc_get_order_status_name($status));

	echo $column_output; ?>

If you would rather just replace the updated orders.php file, copy the code below and replace.

<?php
/**
 * Orders
 *
 * Shows orders on the account page.
 */

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_account_orders', $has_orders ); ?>

<?php if ( $has_orders ) : ?>

	<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">
		<thead>
			<tr>
				<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
					<th class="woocommerce-orders-table__header woocommerce-orders-table__header-<?php echo esc_attr( $column_id ); ?>"><span class="nobr"><?php echo esc_html( $column_name ); ?></span></th>
				<?php endforeach; ?>
			</tr>
		</thead>

		<tbody>
			<?php
			foreach ( $customer_orders->orders as $customer_order ) {
				$order      = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
				$item_count = $order->get_item_count() - $order->get_item_count_refunded();
				?>
				<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-<?php echo esc_attr( $order->get_status() ); ?> order">
					<?php foreach ( wc_get_account_orders_columns() as $column_id => $column_name ) : ?>
						<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-<?php echo esc_attr( $column_id ); ?>" data-title="<?php echo esc_attr( $column_name ); ?>">
							<?php if ( has_action( 'woocommerce_my_account_my_orders_column_' . $column_id ) ) : ?>
								<?php do_action( 'woocommerce_my_account_my_orders_column_' . $column_id, $order ); ?>

							<?php elseif ( 'order-number' === $column_id ) : ?>
								<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
									<?php echo esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ); ?>
								</a>

							<?php elseif ( 'order-date' === $column_id ) : ?>
								<time datetime="<?php echo esc_attr( $order->get_date_created()->date( 'c' ) ); ?>"><?php echo esc_html( wc_format_datetime( $order->get_date_created() ) ); ?></time>

							<?php elseif ('order-status' === $column_id) :

								$status = $order->get_status();
								$column_output = "";

								// show tracking link
								if ($status == 'completed' && class_exists('WC_Shipment_Tracking_Actions')) {

									$st_actions = WC_Shipment_Tracking_Actions::get_instance();

									if ($tracking_items = $st_actions->get_tracking_items($order->get_id(), true)) {

										foreach ($tracking_items as $tracking_item) {

											if ($tracking_item['formatted_tracking_link'] !== '') {

												$column_output = sprintf("<a href='%s' target='_blank'>%s</a>",
													$tracking_item['formatted_tracking_link'], __('Track Shipment', 'bdl'));

												break;
											}
										}
									}
								}

								if (empty($column_output))
									$column_output = esc_html(wc_get_order_status_name($status));

								echo $column_output; ?>

							<?php elseif ( 'order-total' === $column_id ) : ?>
								<?php
								/* translators: 1: formatted order total 2: total order items */
								echo wp_kses_post( sprintf( _n( '%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ) );
								?>

							<?php elseif ( 'order-actions' === $column_id ) : ?>
								<?php
								$actions = wc_get_account_orders_actions( $order );

								if ( ! empty( $actions ) ) {
									foreach ( $actions as $key => $action ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
										echo '<a href="' . esc_url( $action['url'] ) . '" class="woocommerce-button button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>';
									}
								}
								?>
							<?php endif; ?>
						</td>
					<?php endforeach; ?>
				</tr>
				<?php
			}
			?>
		</tbody>
	</table>

	<?php do_action( 'woocommerce_before_account_orders_pagination' ); ?>

	<?php if ( 1 < $customer_orders->max_num_pages ) : ?>
		<div class="woocommerce-pagination woocommerce-pagination--without-numbers woocommerce-Pagination">
			<?php if ( 1 !== $current_page ) : ?>
				<a class="woocommerce-button woocommerce-button--previous woocommerce-Button woocommerce-Button--previous button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page - 1 ) ); ?>"><?php esc_html_e( 'Previous', 'woocommerce' ); ?></a>
			<?php endif; ?>

			<?php if ( intval( $customer_orders->max_num_pages ) !== $current_page ) : ?>
				<a class="woocommerce-button woocommerce-button--next woocommerce-Button woocommerce-Button--next button" href="<?php echo esc_url( wc_get_endpoint_url( 'orders', $current_page + 1 ) ); ?>"><?php esc_html_e( 'Next', 'woocommerce' ); ?></a>
			<?php endif; ?>
		</div>
	<?php endif; ?>

<?php else : ?>
	<div class="woocommerce-message woocommerce-message--info woocommerce-Message woocommerce-Message--info woocommerce-info">
		<a class="woocommerce-Button button" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
			<?php esc_html_e( 'Go to the shop', 'woocommerce' ); ?>
		</a>
		<?php esc_html_e( 'No order has been made yet.', 'woocommerce' ); ?>
	</div>
<?php endif; ?>

<?php do_action( 'woocommerce_after_account_orders', $has_orders ); ?>

Summary

The WooCommerce Shipment Tracking plugin is a easy to use plugin if you need a solid way to allow your customers to track their shipments.

Using this plugin is a fun little way to give your customers a little bit more insight when their orders coming and allow to engage more with your brand as a customer.

Resources

Comments

  1. Wesley Townie August 13, 2019 | 11:46 am

    Hi Simon, thanks for the tutorial. I have the plugin setup but curious how I can copy and override the orders.php file in my theme. I am stuck on how to do this. Any insight?

Leave a reply

Your email address will not be published. Required fields are marked*