Track My Orders Using Order Status in Magento 2
- Pending as Ordered
- Processing as Accepted
- Shipping as On It's Way
- Completed as Delivered
Example\Track\Controller\Order\Index.php
<?php
/**
* Copyright © magento2.
*/
namespace Example\Track\Controller\Order;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Sales\Controller\OrderInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends \Magento\Framework\App\Action\Action implements OrderInterface, HttpGetActionInterface
{
/**
*
* @var PageFactory
*
*/
protected $resultPageFactory;
/**
*
* @param Context $context
* @param PageFactory $resultPageFactory
*
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Customer order history
*
* @return \Magento\Framework\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->set(__('Track My Order'));
$block = $resultPage->getLayout()->getBlock('customer.account.link.back');
return $resultPage;
}
}
Example\Track\etc\module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © magento2.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Example_Track"></module>
</config>
Example\Track\etc\frontend\routes.xml
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route frontName="track" id="track">
<module name="Example_Track"/>
</route>
</router>
</config>
Example\Track\view\frontend\layout\track_order_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © magento2.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="customer_account"/>
<referenceContainer name="content">
<block class="Magento\Sales\Block\Order\History" name="track_order_index" template="Example_Track::order/index.phtml" cacheable="false" />
</referenceContainer>
</page>
Example\Track\view\frontend\templates\index.phtml
<?php
/**
* Copyright © Magento2.
*/
/** @var \Example\Track\Block\Order\Index $block */
?>
<?php
$_orders = $block->getOrders();
?>
<?= $block->getChildHtml('info') ?>
<?php if ($_orders && count($_orders)): ?>
<div class="table-wrapper orders-history">
<table>
<tbody>
<?php foreach ($_orders as $_order): ?>
<tr>
<td><?= /* @noEscape */ date("l jS F Y", strtotime($_order->getCreatedAt())) ?></td>
<td><b><?= $block->escapeHtml(__('#ID ')) ?>
<?= $block->escapeHtml($_order->getRealOrderId()) ?></b></td>
<td><?= $block->escapeHtml(__('TOTAL PAID')) ?></td>
<td><b><?= /* @noEscape */ $_order->formatPrice($_order->getGrandTotal()) ?></b></td>
</tr>
<tr>
<td><svg class="tracking-icons " width="72.75" height="72.75" viewBox="0 0 72.75 72.75">
<g transform="translate(-1381.85 -621)">
<g class="circle" transform="translate(1381.85 621)"
fill="none" stroke="#04823b" stroke-miterlimit="10" stroke-width="4">
<circle cx="36.375" cy="36.375" r="36.375" stroke="none"></circle>
<circle class="border" cx="36.375" cy="36.375" r="34.375" fill="none"></circle>
</g>
<path class="tick" d="M3476.24,9228.867l-9.674-9.673,3.225-3.224,6.449,6.448,
14.51-14.51,3.224,3.224Z" transform="translate(-2062.045 -8561.666)" fill="#04823b">
</path>
</g>
</svg>
</td>
<td><?= $block->escapeHtml(__('Ordered')) ?></td>
<?php if ($_order->hasShipments() || $_order->getStatus() == 'processing'
|| $_order->getStatus() == 'complete'): ?>
<td><svg class="tracking-icons " width="72.75" height="72.75" viewBox="0 0 72.75 72.75">
<g transform="translate(-1381.85 -621)">
<g class="circle" transform="translate(1381.85 621)"
fill="none" stroke="#04823b" stroke-miterlimit="10" stroke-width="4">
<circle cx="36.375" cy="36.375" r="36.375" stroke="none"></circle>
<circle class="border" cx="36.375" cy="36.375" r="34.375" fill="none"></circle>
</g>
<path class="tick" d="M3476.24,9228.867l-9.674-9.673,3.225-3.224,6.449,6.448,
14.51-14.51,3.224,3.224Z" transform="translate(-2062.045 -8561.666)" fill="#04823b">
</path>
</g>
</svg>
</td>
<?php else: ?>
<td><svg class="tracking-icons " width="72.75" height="72.75" viewBox="0 0 72.75 72.75">
<g transform="translate(-1381.85 -621)">
<g class="circle" transform="translate(1381.85 621)"
fill="none" stroke="#e5e5e4" stroke-miterlimit="10" stroke-width="4">
<circle cx="36.375" cy="36.375" r="36.375" stroke="none"></circle>
<circle class="border" cx="36.375" cy="36.375" r="34.375" fill="none"></circle>
</g>
<path class="tick" d="M3476.24,9228.867l-9.674-9.673,3.225-3.224,6.449,6.448,
14.51-14.51,3.224,3.224Z" transform="translate(-2062.045 -8561.666)" fill="#cbcaca">
</path>
</g>
</svg>
</td>
<?php endif; ?>
<td><?= $block->escapeHtml(__("Accepted")) ?></td>
<?php if ($_order->hasShipments()): ?>
<td><svg class="tracking-icons " width="72.75" height="72.75" viewBox="0 0 72.75 72.75">
<g transform="translate(-1381.85 -621)">
<g class="circle" transform="translate(1381.85 621)"
fill="none" stroke="#04823b" stroke-miterlimit="10" stroke-width="4">
<circle cx="36.375" cy="36.375" r="36.375" stroke="none"></circle>
<circle class="border" cx="36.375" cy="36.375" r="34.375" fill="none"></circle>
</g>
<path class="tick" d="M3476.24,9228.867l-9.674-9.673,3.225-3.224,6.449,6.448,
14.51-14.51,3.224,3.224Z" transform="translate(-2062.045 -8561.666)" fill="#04823b">
</path>
</g>
</svg>
</td>
<?php else: ?>
<td><svg class="tracking-icons " width="72.75" height="72.75" viewBox="0 0 72.75 72.75">
<g transform="translate(-1381.85 -621)">
<g class="circle" transform="translate(1381.85 621)"
fill="none" stroke="#e5e5e4" stroke-miterlimit="10" stroke-width="4">
<circle cx="36.375" cy="36.375" r="36.375" stroke="none"></circle>
<circle class="border" cx="36.375" cy="36.375" r="34.375" fill="none"></circle>
</g>
<path class="tick" d="M3476.24,9228.867l-9.674-9.673,3.225-3.224,6.449,6.448,
14.51-14.51,3.224,3.224Z" transform="translate(-2062.045 -8561.666)" fill="#e5e5e4">
</path>
</g>
</svg>
</td>
<?php endif; ?>
<td><?= $block->escapeHtml(__("On it's Way")) ?></td>
<?php if ($_order->hasShipments() && $_order->getStatus() == 'complete'): ?>
<td><svg class="tracking-icons " width="72.75" height="72.75" viewBox="0 0 72.75 72.75">
<g transform="translate(-1381.85 -621)">
<g class="circle" transform="translate(1381.85 621)"
fill="none" stroke="#04823b" stroke-miterlimit="10" stroke-width="4">
<circle cx="36.375" cy="36.375" r="36.375" stroke="none"></circle>
<circle class="border" cx="36.375" cy="36.375" r="34.375" fill="none"></circle>
</g>
<path class="tick" d="M3476.24,9228.867l-9.674-9.673,3.225-3.224,6.449,6.448,
14.51-14.51,3.224,3.224Z" transform="translate(-2062.045 -8561.666)" fill="#04823b">
</path>
</g>
</svg>
</td>
<?php else: ?>
<td><svg class="tracking-icons " width="72.75" height="72.75" viewBox="0 0 72.75 72.75">
<g transform="translate(-1381.85 -621)">
<g class="circle" transform="translate(1381.85 621)"
fill="none" stroke="#e5e5e4" stroke-miterlimit="10" stroke-width="4">
<circle cx="36.375" cy="36.375" r="36.375" stroke="none"></circle>
<circle class="border" cx="36.375" cy="36.375" r="34.375" fill="none"></circle>
</g>
<path class="tick" d="M3476.24,9228.867l-9.674-9.673,3.225-3.224,6.449,6.448,
14.51-14.51,3.224,3.224Z" transform="translate(-2062.045 -8561.666)" fill="#e5e5e4">
</path>
</g>
</svg>
</td>
<?php endif; ?>
<td><?= $block->escapeHtml(__("Delivered")) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php else: ?>
<div><?= $block->escapeHtml(__("You don't have any orders yet!")) ?></div>
<?php endif ?>
<?php if ($block->getPagerHtml()): ?>
<div class="orders-pagination"><?= $block->getPagerHtml(); ?></div>
<?php endif ?>
Example\Track\registration.php
<?php
/**
* Copyright © Magento2.
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Example_Track',
__DIR__
);