Order tracking information on note emails

Places the ShipStation order tracking information from ShipStation onto Customer Note emails. Supports USPS, UPS, and FedEx.

add_action( 'woocommerce_email_header', function( $email_heading, $email ) {

	// Limit To Specific Email
	if(
		empty( $email->id )
		|| ! in_array( $email->id, [ 'customer_note' ] )
	) {
		return;
	}

	// Get Order
	$order = $email->object;
	if( ! $order ) {
		return;
	}

	// Translate Email Copy
	add_filter( 'gettext', function( $text ) use( $order ) {

		// Specific Line
		if( $text != 'As a reminder, here are your order details:' ) {
			return $text;
		}

		// Loop Customer Order Notes
		$notes = wc_get_order_notes(
			[
				'order_id' => $order->get_id(),
				'type' => 'customer',
			]
		);
		foreach( $notes as $note ) {

			// Match ShipStation Formatted Note
			if( stristr( $note->content, ' shipped via ' ) === false ) {
				continue;
			}

			// Get USPS Tracking Number
			if( preg_match( '/\b(\d{20,22})\b/', $note->content, $matches ) ) {
				$tracking_num = $matches[1];
				$note->content = str_replace( 'USPS', '<strong>USPS</strong>', $note->content );
				$tracking_link = 'https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1';
			}

			// Detect UPS
			if( preg_match( '/\b1Z[A-Z0-9]{16}\b/i', $note->content, $matches ) ) {
				$tracking_num = $matches[1];
				$note->content = str_replace( 'UPS', '<strong>UPS</strong>', $note->content );
				$tracking_link = 'https://www.ups.com/track?loc=en_US&tracknum';
			}

			// Detect FedEx
			if( stristr( $note->content, 'FedEx' ) ) {
				$words = preg_replace( "/[^A-Za-z0-9 ]/", '', $note->content);
				$words = explode( ' ', $words );
				$last_index = sizeof( $words ) - 1;
				$tracking_num = $words[$last_index];
				$note->content = str_replace( 'FedEx', '<strong>FedEx</strong>', $note->content );
				$tracking_link = 'https://www.fedex.com/fedextrack/?trknbr';
			}

			// Missing Tracking Link
			if( empty( $tracking_link ) ) {
				continue;
			}

			// Get Tracking Link
			$tracking_link = sprintf(
				'<a target="_blank" href="%s=%s">%s</a>',
				$tracking_link,
				$tracking_num,
				$tracking_num
			);

			// Output Tracking Link
			printf(
				'<p><strong>Track your package: %s</strong></p>',
				$tracking_link
			);

			// Partial Shipment Text
			if( preg_match_all( '/ x (?<qty>\d+)/', $note->content, $matches ) ) {
				$shipment_qty = array_sum( $matches['qty'] );
				if( $shipment_qty != $order->get_item_count() ) {
					printf(
						'
							<p>
								<strong style="color:IndianRed;">
									** Partial shipment quantity %d of %d. **
								</strong>
							</p>
						',
						$shipment_qty,
						$order->get_item_count()
					);
				}
			}

		} // End Customer Notes Loop

		// Preserve Original Translation
		return $text;

	} ); // End Translate Email Copy

}, 10, 2 ); // End Email Header

Instructions for Order tracking information on note emails

  1. Log into a staging or locally hosted clone of your site.
  2. Install and activate Code Snippets plugin.
  3. WP Admin > Snippets > Add New
  4. Copy and paste the code from the section above.
  5. Check to ensure formatting came over properly.
  6. Customize the code as desired.
  7. Add a meaningful title.
  8. Select whether to run on front-end or back-end (or both).
  9. Click “Save and Activate”.
  10. Test your site to ensure it works.
  11. Disable if any problems, or recover.
  12. Repeat for live environment.

Need help modifying Order tracking information on note emails?

Contact me. I can help with fitting projects or refer to my partner.

License

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.

Disclaimer of warranty