2

I want to add a new action in My Orders page that is Delete action enter image description here

How can I do that? Can anyone help me with this function?

1

You need to create a following files

Create this file at Path app/code/Vendor/Module/view/frontend/layout/sales_order_history.xml

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">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">Vendor_Module::order/history.phtml</argument>
            </action>
        </referenceBlock>
    </body>
</page>

history.phtml

Copy this file from Path vendor/magento/module-sales/view/frontend/templates/order/history.phtml

to your module

app/code/Vendor/Module/view/frontend/templates/order/history.phtml

Add this code to after line no. 43, instead of this code $block->getViewUrl($_order) you can write your logic to delete order.

<a href="<?= /* @escapeNotVerified */ $block->getViewUrl($_order) ?>" class="action delete">
    <span><?= /* @escapeNotVerified */ __('Delete') ?></span>
</a>

here is whole code of history.phtml

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?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>
                </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>
                            <a href="<?= /* @escapeNotVerified */ $block->getViewUrl($_order) ?>" class="action delete">
                                <span><?= /* @escapeNotVerified */ __('Delete') ?></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>
                    </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 ?>
  • Thank you for your kindly answer my question. It's the first time I create a new function in Magento 2, can you spend a little time to tell me about this function? How can I delete my orders – Kite Hany Jul 4 '19 at 1:31
  • ok let me make the changes – Mohit Rane Jul 4 '19 at 5:33
  • Yes, I will wait for your answer while I am researching – Kite Hany Jul 4 '19 at 6:27
  • ok. I'm still working on it. Made some changes to delete order but need to pass it as url on delete action. – Mohit Rane Jul 4 '19 at 7:41
  • did you find solution to delete order ? – Mohit Rane Jul 5 '19 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.