Magento 2:使用订单历史记录页面上的订单添加跟踪订单链接
  • Magento
  • 2023-08-31 21:38:10
  • 30026 阅读

如何在Magento 2中将订单历史记录页面上的订单链接添加跟踪订单:

将主题中的供应商/magento/module-sales/view/frontend/templates/order/history.phtml 文件复制到以下路径下:

app/design/frontend/themees/yourtheme/Magento_Sales/templates/order/history.phtml

现在,添加以下代码:

<?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 ?>

更改后刷新缓存。

通过上述代码的实现,跟踪订单列链接将添加到订单主页,如下所示:

跟踪顺序

结论:

期待的是,所有人都能够 添加跟踪订单 链接与Magento 2中的订单历史记录页面上的订单。除此之外,使用Magento 2的订单跟踪扩展来增强商店的客户体验,该扩展可以直接从客户的“我的帐户”部分跟踪订单。