TrackShip for WooCommerce Shortcode

Below, you’ll find a detailed guide on how to add the TrackShip for WooCommerce Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the TrackShip for WooCommerce Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the TrackShip for WooCommerce Plugin and the shortcodes it provides:

Plugin Icon
TrackShip for WooCommerce

"TrackShip for WooCommerce is an essential plugin that seamlessly integrates with your WooCommerce store, enabling easy monitoring and management of your shipments, ensuring a smooth customer experience."

★★★★☆ (25) Active Installs: 5000+ Tested with: 6.3.0 PHP Version: 7.2
Included Shortcodes:
  • [wcast-track-order]

TrackShip for WooCommerce [wcast-track-order] Shortcode

The TrackShip for WooCommerce shortcode is a tool that allows users to track their orders. It checks if TrackShip is active, validates the order ID and key, and displays tracking information. The PHP function ‘woo_track_order_function’ linked to this shortcode retrieves the order based on the ID and key from the URL. If the order is valid, it fetches the tracking details from the ‘trackship_shipment’ table. If the order or tracking details are not found, it returns an error message. If everything is correct, it displays the tracking page with the order’s tracking items. If no tracking items are found, it resets the order ID. The function ends by returning a form with relevant tracking details or an error message.

Shortcode: [wcast-track-order]

Examples and Usage

Basic example – A simple usage of the shortcode to track a WooCommerce order. It does not require any parameters/attributes.

[wcast-track-order /]

PHP Function Code

In case you have difficulties debugging what causing issues with [wcast-track-order] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'wcast-track-order', array( $this, 'woo_track_order_function') );

Shortcode PHP function:

function woo_track_order_function() {
		
		wp_enqueue_style( 'front_style' );
		wp_enqueue_script( 'jquery-blockui' );
		wp_enqueue_script( 'front-js' );	
		
		if ( ! is_trackship_connected() ) { ?>
			<p><a href="https://trackship.com/" target="blank">TrackShip</a> is not active.</p>
			<?php
			return;
		}
		
		if ( isset( $_GET['order_id'] ) &&  isset( $_GET['order_key'] ) ) {
			
			$order_id = wc_clean($_GET['order_id']);
			$order = wc_get_order( $order_id );
			
			if ( empty( $order ) ) {
				$error = new WP_Error( 'ts4wc', __( 'Unable to locate the order.', 'trackship-for-woocommerce' ) );
			} else {
				
				$order_key = $order->get_order_key();
			
				if ( $order_key != $_GET['order_key'] ) {
					$error = new WP_Error( 'ts4wc', __( 'Unable to locate the order. or Invalid order key', 'trackship-for-woocommerce' ) );
				}
				
			}
		}

		if ( isset( $_GET['tracking'] ) ) {
			$_GET['tracking'];
			global $wpdb;
			$shipment_table = $wpdb->prefix . 'trackship_shipment';

			$tracking_number = wc_clean( $_GET[ 'tracking' ] );
			$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT order_id FROM $shipment_table WHERE tracking_number = %s", $tracking_number ) );
			$order = wc_get_order( $order_id );
			if ( empty( $order ) ) {
				$error = new WP_Error( 'ts4wc', __( 'Unable to locate the order.', 'trackship-for-woocommerce' ) );
			}
		}
	
		if ( ! isset( $order_id ) || empty( $order ) || isset( $error ) ) {

			if ( isset( $error ) && is_wp_error( $error ) ){
				echo $error->get_error_message();
			}

			ob_start();		
			$this->track_form_template();
			$form = ob_get_clean();	
			return $form;

		} else {

			$tracking_items = trackship_for_woocommerce()->get_tracking_items( $order_id );
			if ( !$tracking_items ) {
				unset( $order_id );
			}

			ob_start();												
			echo esc_html( $this->display_tracking_page( $order_id, $tracking_items ) );
			$form = ob_get_clean();	
			return $form;		
		}		
	}

Code file location:

trackship-for-woocommerce/trackship-for-woocommerce/includes/class-wc-trackship-front.php

Conclusion

Now that you’ve learned how to embed the TrackShip for WooCommerce Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

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