cancel
Showing results for 
Search instead for 
Did you mean: 

How to hide old orders from account > My Orders page

How to hide old orders from account > My Orders page

Hi guys,

I want to hide old orders from customer accounts page. For example hide more then 6 months old orders.

/public_html/app/design/frontend/VENDOR/layout01/Magento_Sales/templates/order/history.phtml

<?php
/**
 * Copyright © magebig.com - All rights reserved.
 * See LICENSE.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">
            <thead>
                <tr>
                    <th scope="col" class="table-col order-id"><?= $block->escapeHtml(__('Order #')) ?></th>
                    <th scope="col" class="table-col date"><?= $block->escapeHtml(__('Date')) ?></th>
                    <?= /* @noEscape */ $block->getChildHtml('extra.column.header') ?>
                    <th scope="col" class="table-col shipping"><?= $block->escapeHtml(__('Ship To')) ?></th>
                    <th scope="col" class="table-col total"><?= $block->escapeHtml(__('Order Total')) ?></th>
                    <th scope="col" class="table-col status"><?= $block->escapeHtml(__('Status')) ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($_orders as $_order): ?>
                    <tr>
                        <td data-th="<?= $block->escapeHtml(__('Order')) ?>" class="table-col order-id">
                            <a href="<?= $block->getViewUrl($_order) ?>" class="action view mb-tooltip"
                               title="<?= $block->escapeHtml(__('View Order')) ?> #<?= $_order->getRealOrderId() ?>">
                                <span><?= $_order->getRealOrderId() ?></span>
                            </a>
                        </td>
                        <td data-th="<?= $block->escapeHtml(__('Date')) ?>" class="table-col date"><?= $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="table-col shipping"><?= $_order->getShippingAddress() ? $block->escapeHtml($_order->getShippingAddress()->getName()) : '&nbsp;' ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Order Total')) ?>" class="table-col total"><?= $_order->formatPrice($_order->getGrandTotal()) ?></td>
                        <td data-th="<?= $block->escapeHtml(__('Status')) ?>" class="table-col status">
                            <a href="<?= $block->getViewUrl($_order) ?>" class="action-status mb-tooltip"
                               title="<?= $block->escapeHtml(__('View Order')) ?> #<?= $_order->getRealOrderId() ?>">
                                <span class="order-status <?= $_order->getState() ?>"><?= $_order->getStatusLabel() ?></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><?= $block->escapeHtml(__('You have placed no orders.')) ?></span></div>
<?php endif ?>

 

123123.png

screenshot : https://ibb.co/pJd3K7Y

we are using magento 2.4.3

3 REPLIES 3

Re: How to hide old orders from account > My Orders page

In the function getOrders (vendor/magento/module-sales/Block/Order/History.php:97) you can add conditions to get the desired records
public function getOrders()

{
if (!($customerId = $this->_customerSession->getCustomerId())) {
return false;
}
if (!$this->orders) {
$this->orders = $this->getOrderCollectionFactory()->create($customerId)->addFieldToSelect(
'*'
)->addFieldToFilter(
'status',
['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
)->addFieldToFilter('created_at', array('gteq' => $fromDate)
)->addFieldToFilter('created_at', array('lteq' => $toDate)
)->setOrder(
'created_at',
'desc'
);
}
return $this->orders;
}
If issue solved, Click Kudos & Accept as Solution.
LitCommerce - The Most Simple & Affordable Multi-channel Selling Tool

Re: How to hide old orders from account > My Orders page

Not worked i am afraid.

Re: How to hide old orders from account > My Orders page

Use plugins classes since the method is public of class vendor/magento/module-sales/Block/Order/History.php

 

function name public function getOrders()

 

https://www.magecheck.com/magento-2-create-plugin use 'after' method of plugin class and modify the output of getOrders() function