Fastest Way to Add Track My Order Function to WooCommerce Store with Dianxiaomi ERP Support

Most ERPs suitable for WooCommerce allow store owners to manage orders conveniently and do not support customers to query tracking information without logging in. For example, Shopkeeper ERP can easily manage orders, and there is no separate page for customers to Query the logistics status of the order. In this article, you can learn quickly create a Track My Order shortcode and use the order ID and email to query the shipping provider name and the tracking number of the order.

To create a shortcode without modifying the original theme for your store, you have to create a child theme for the current theme. Now in the functions.php file and add the code below.


add_shortcode('track_my_order', 'track_my_order_function');

function track_my_order_function()
{
    ob_start();
    ?>
    <div class="check-order-tracking">
        <form action="" method="post">
            <div class="form-group">
                <label for="order_id">Order ID</label>
                <input type="text" name="order_id" id="order_id" class="form-control" placeholder="Enter Order ID" required value="<?php echo $_POST['order_id']; ?>">
            </div>
            <div class="form-group">
                <label for="order_email">Email</label>
                <input type="email" name="order_email" id="order_email" class="form-control" placeholder="Enter Email" required value="<?php echo $_POST['order_email']; ?>">
            </div>
            <div class="form-group text-center">
                <button type="submit" class="button success expand">Track Now</button>
            </div>
        </form>
    </div>
<?php
    if (isset($_POST['order_id']) && isset($_POST['order_email'])) {
        $order_id = $_POST['order_id'];
        $order_email = $_POST['order_email'];
        $order = wc_get_order($order_id);
        if ($order) {
            $order_data = $order->get_data();
            $billing_email = $order_data['billing']['email'];
            $provider_name = $order->get_meta('_dianxiaomi_tracking_provider_name');
            if ($order_email == $billing_email) {
                if ($provider_name) {
                    $tracking_number = $order->get_meta('_dianxiaomi_tracking_number');
                    echo '<div class="success" style="padding: 10px; color: white;border-radius: 5px;" role="alert">Your order is shipped by ' . $provider_name . ' with tracking number <a target="_blank" style="color:white; font-weight: bold;" href="https://t.17track.net/en#nums=' . $tracking_number . '">' . $tracking_number . '</a>.</div>';
                } else {
                    echo '<div class="alert" style="padding: 10px; color: white;border-radius: 5px;" role="alert">Your order is not shipped yet.</div>';
                }
            } else {
                echo '<div class="alert alert-danger" style="padding: 10px; color: white;border-radius: 5px;" role="alert">Your order id or email is not correct.</div>';
            }
        } else {
            echo '<div class="alert alert-danger" style="padding: 10px; color: white;border-radius: 5px;" role="alert">Your order id or email is not correct.</div>';
        }
    }
    return ob_get_clean();
}

Use the shortcode

Now you can use the shortcode [track_my_order] in any page or post of your store. After submitting the correct order ID and Email, the tracking provider name and the tracking number will be shown below the check now button. When clicking on the tracking number, it will jump to the 17tracking website for more details.

Frequently Asked Questions

Does the code work on my theme?

The code above works with the WooCommerce store on any theme if you’re using Dianxiaomi ERP to manage the WooCommerce orders.

Why don’t we try some plugins?

  1. We don’t want to install too many plugins, which would bloat our store.
  2. The code above is much lighter and safer than installing a new plugin.
  3. Dianxiaomi ERP is not using the standard name of the post meta, so we have to rewrite the functions to get the provider name and the tracking number of the order.

Leave a Reply

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