I want to show track order link in My Account > My Orders.How to give link to that.How to add functionality for that.Help me
2 Answers
Possible solution based on Magento 2.3.2:
app/code/Order/Tracking/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Order_Tracking"/>
</config>
app/code/Order/Tracking/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Order_Tracking',
__DIR__
);
app/code/Order/Tracking/view/frontend/layout/sales_order_history.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="sales.order.history">
<arguments>
<argument name="tracking_view_model" xsi:type="object">Order\Tracking\ViewModel\Tracking</argument>
</arguments>
<action method="setTemplate">
<argument name="template" xsi:type="string">Order_Tracking::order/history.phtml</argument>
</action>
</referenceBlock>
</body>
</page>
app/code/Order/Tracking/view/frontend/templates/order/history.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var \Magento\Sales\Block\Order\History $block */
/** @var \Order\Tracking\ViewModel\Tracking $trackingModel */
?>
<?php $_orders = $block->getOrders(); ?>
<?php $trackingModel = $block->getData('tracking_view_model'); ?>
<?= $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>
</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()) : ' ' ?></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 ?>
<?php if ($_order->getShipmentsCollection()->getSize()): ?>
<a href="#"
class="action order"
data-mage-init='{"popupWindow": {"windowURL":"<?= /* @escapeNotVerified */ $trackingModel->getTrackingPopupUrlBySalesModel($_order) ?>","windowName":"trackorder","width":800,"height":600,"left":0,"top":0,"resizable":1,"scrollbars":1}}'>
<span><?= /* @escapeNotVerified */ __('Track your order') ?></span>
</a>
<?php endif ?>
</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;?>
app/code/Order/Tracking/ViewModel/Tracking.php
<?php
namespace Order\Tracking\ViewModel;
use Magento\Framework\View\Element\Block\ArgumentInterface;
class Tracking implements ArgumentInterface
{
/**
* @var \Magento\Shipping\Helper\Data
*/
private $shippingHelper;
/**
* Tracking constructor.
* @param \Magento\Shipping\Helper\Data $shippingHelper
*/
public function __construct(\Magento\Shipping\Helper\Data $shippingHelper)
{
$this->shippingHelper = $shippingHelper;
}
public function getTrackingPopupUrlBySalesModel($order)
{
return $this->shippingHelper->getTrackingPopupUrlBySalesModel($order);
}
}
-
i am getting error at Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\porto\app\code\Order\Tracking\view\frontend\templates\order\history.phtml on line 73 Commented Feb 8, 2020 at 11:39
Functionality should already be in Magento 2.
It works in a similar way to Magento 1.
You can point your link to /sales/guest/form/
eg.:
https://example.com/sales/guest/form/
This page should display form which asks for the order number, billing last name, email or zip code.