Categories: How ToMagento 2

Magento 2: Add Track Order link with the Order on Order History Page

Hello Magento Friends 👋,

Welcome back to Magento Technical Blog series. Today I will throw light on Magento 2: Add Track Order link with the Order on Order History Page. Look at what our previous blog has to say, How to add Stock Status column in Admin Product Grid Magento 2.

As soon as the customer places an order, he/she is eager to know about when their products will arrive. An order tracking process allows the customer to know the status of their orders like pending, processing, shipped, delivered. My orders section displays the order information that comprises information like the order number, date, amount, status, etc. What if you want to provide an order tracking link to the customers of your Magento 2 store into their My Orders page?

Let us know How to Add Track Order link with the Order on Order History Page in Magento 2 🚀

How to Add Track Order link with the Order on Order History Page in Magento 2:

Copy the vendor/magento/module-sales/view/frontend/templates/order/history.phtml file in your theme under the below path:

app/design/frontend/Themes/Yourtheme/Magento_Sales/templates/order/history.phtml 

Now, add the below code:

<?php $_orders = $block->getOrders(); ?>
<?= $block->getChildHtml('info') ?>
<?php if ($_orders && count($_orders)): ?>
    <div class="table-wrapper orders-history">
        <table class="data table table-order-items history" id="my-orders-table">
            <caption class="table-caption"><?= /* @escapeNotVerified */                __('Orders') ?></caption>
            <thead>
            <tr>
                <th scope="col" class="col id"><?= /* @escapeNotVerified */                    __('Order #') ?></th>
                <th scope="col" class="col date"><?= /* @escapeNotVerified */                    __('Date') ?></th>
                <?= /* @noEscape */                $block->getChildHtml('extra.column.header') ?>
                <th scope="col" class="col shipping"><?= /* @escapeNotVerified */                    __('Ship To') ?></th>
                <th scope="col" class="col total"><?= /* @escapeNotVerified */                    __('Order Total') ?></th>
                <th scope="col" class="col status"><?= /* @escapeNotVerified */                    __('Status') ?></th>
                <th scope="col" class="col actions"><?= /* @escapeNotVerified */                    __('Action') ?></th>
                <th scope="col" class="col actions">Track Order</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($_orders as $_order): ?>
                <tr>
                    <td data-th="<?= $block->escapeHtml(__('Order #')) ?>" class="col id"><?= /* @escapeNotVerified */                        $_order->getRealOrderId() ?></td>
                    <td data-th="<?= $block->escapeHtml(__('Date')) ?>" class="col date"><?= /* @escapeNotVerified */                        $block->formatDate($_order->getCreatedAt()) ?></td>
                    <?php $extra = $block->getChildBlock('extra.container'); ?>
                    <?php if ($extra): ?>
                        <?php $extra->setOrder($_order); ?>
                        <?= /* @noEscape */                        $extra->getChildHtml() ?>
                    <?php endif; ?>
                    <td data-th="<?= $block->escapeHtml(__('Ship To')) ?>"
                        class="col shipping"><?= $_order->getShippingAddress() ? $block->escapeHtml($_order->getShippingAddress()->getName()) : '&nbsp;' ?></td>
                    <td data-th="<?= $block->escapeHtml(__('Order Total')) ?>"
                        class="col total"><?= /* @escapeNotVerified */                        $_order->formatPrice($_order->getGrandTotal()) ?></td>
                    <td data-th="<?= $block->escapeHtml(__('Status')) ?>"
                        class="col status"><?= /* @escapeNotVerified */                        $_order->getStatusLabel() ?></td>
                    <td data-th="<?= $block->escapeHtml(__('Actions')) ?>" class="col actions">
                        <a href="<?= /* @escapeNotVerified */                        $block->getViewUrl($_order) ?>" class="action view">
                            <span><?= /* @escapeNotVerified */                                __('View Order') ?></span>
                        </a>
                        <?php if ($this->helper('Magento\Sales\Helper\Reorder')->canReorder($_order->getEntityId())) : ?>
                            <a href="#" data-post='<?php /* @escapeNotVerified */                            echo
                            $this->helper(\Magento\Framework\Data\Helper\PostHelper::class)
                                ->getPostData($block->getReorderUrl($_order))
                            ?>' class="action order">
                                <span><?= /* @escapeNotVerified */                                    __('Reorder') ?></span>
                            </a>
                        <?php endif ?>
                    </td>
                    <td class="col actions">
                        <a href="#" class="action track" title="Track Your Order"
                           data-mage-init='{"popupWindow": {"windowURL":"<?= $this->helper('\Magento\Shipping\Helper\Data')->getTrackingPopupUrlBySalesModel($_order); ?>","windowName":"trackorder","width":800,"height":600,"left":0,"top":0,"resizable":1,"scrollbars":1}}'>
                            <span>Track Your Order</span>
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php if ($block->getPagerHtml()): ?>
        <div class="order-products-toolbar toolbar bottom"><?= $block->getPagerHtml() ?></div>
    <?php endif ?>
<?php else: ?>
    <div class="message info empty"><span><?= /* @escapeNotVerified */            __('You have placed no orders.') ?></span></div>
<?php endif ?>

 Flush the cache after the changes. That’s it.

With the implementation of the above code, the track order column link will be added to the order home page as shown below:

Conclusion:

Expectantly, all are able to Add Track Order link with the Order on Order History Page in Magento 2. Apart from this, increase the customer experience of your store with Order Tracking Extension for Magento 2, which enables tracking order directly from the customer’s My Account section.

In case of any issues while implementing the code, notify me in the comment section below. Do share the article with your Magento colleagues and friends. Keep in touch for more such solutions.

Happy Coding 😊

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

4 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago