Code Snippets

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.
admin

Share
Published by
admin

Recent Posts

OpenAI Tools for WordPress & WooCommerce – New Plugin Released

Attention all e-commerce business owners and bloggers, we're excited to introduce the latest WordPress plugin…

1 year ago

How to get order notifications from Multi-WooCommerce Stores?

Keeping track of orders across multiple WooCommerce stores can be a hassle, but with WooCat…

1 year ago

How to add a WooCommerce Store to WooCat?

Are you looking for an easy way to manage your WooCommerce stores? Look no further…

1 year ago

Version 2.5.2 Released of Auto Bulb Finder Plugin On WordPress.org

After several times of reviewing by the WordPress Team, Auto Bulb Finder for WordPress &…

1 year ago

Version 2.4.0 Released of Auto Bulb Finder Plugin for WordPress

In this update, the Store Only feature is brought to the Auto Bulb Finder Plugin.…

1 year ago

Version 2.2.5 Released of Auto Bulb Finder Plugin for WordPress

Search Similar Bulb Size New Features You can easily search the similar bulb size name…

1 year ago

This website uses cookies.