Implementing Order Search Functionality in Customer My Orders Section of Magento
As an experienced developer at Laravel Company, I often help our clients implement customer-centric features like order search within their Magento stores. In this post, we’ll guide you through adding a search box to the “My Orders” section for customers to easily find specific orders without navigating through multiple pages.
Step 1: Understanding the Customer’s Current Order History Page
The default order/history.phtml template in the Magento_Sales/templates/order/ directory is where we’ll begin our modifications. This file currently displays a list of all orders associated with the logged-in customer. However, it doesn’t provide any search functionality out of the box.
Here’s what the default template structure looks like:
<!-- Magento_Sales/templates/order/history.phtml -->
<div class="page orderhistory">
<div class="maincolumns">
<!-- ... existing content ... -->
<h1 class="heading">
<?php /* @escapeNotVerified */ echo __('My Orders') ?>
</h1>
<div class="table-wrapper">
<?php if (count($orders) > 0): ?>
<table class="data table order-table">
<!-- ... existing table structure with orders data ... -->
</table>
<?php else: ?>
<div class="no-records">
<?php /* @escapeNotVerified */ echo __('No Orders') ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
Step 2: Adding the Search Box to the Template
To add a search box, we’ll modify this history.phtml template. Insert the following code block above the table, or within the <h1> heading:
<div class="search-wrapper">
<form action="" method="GET" id="search-order-form">
<input type="text" name="order_search" id="order-search-input"
placeholder="<?php /* @escapeNotVerified */ echo __('Search Orders') ?>"/>
<button type="submit"><?php /* @escapeNotVerified */ echo __('Search') ?></button>
</form>
</div>
<script>
require(['jquery'], function ($) {
$('#search-order-form').on('submit', function (e) {
e.preventDefault();
var searchText = $('#order-search-input').val();
if (searchText !== '') {
window.location.href = '<?php /* @escapeNotVerified */ echo $this->getUrl(
'sales/order/history'
) ?>?order_search=' + encodeURIComponent(searchText);
}
});
});
</script>
This code adds a search input field with an associated button. It uses jQuery to handle the form submission and updates the URL with the encoded search text.
Step 3: Modifying the Controller to Filter Orders
The next step is to modify the Sales/OrderController.php file in the Magento/Sales/Controller/Adminhtml/ directory to filter orders based on the search query parameter.
Add the following lines within the execute() method:
if ($this->getRequest()->getParam('order_search')) {
$searchText = $this->getRequest()->getParam('order_search');
$orders = $this->_objectManager->create('Magento\Sales\Model\Order')
->addFieldToSelect('*')
->addFieldToFilter(['increment_id', 'real_order_id'], ['like' => '%'.$searchText.'%'])
->setPageSize(null)
->getCollection();
} else {
$orders = $this->_orderRepository->getList($searchCriteria);
}
This code snippet checks if the order_search parameter is present in the request. If it is, it uses this text to filter orders by order ID (increment_id and real_order_id) and retrieves the filtered collection.
Step 4: Updating the Template with the Filtered Orders
Finally, we need to update the history.phtml template to use the filtered order collection in step 3. Replace the existing table structure with:
<?php if (count($orders) > 0): ?>
<table class="data table order-table">
<!-- ... existing table header ... -->
<?php foreach ($orders as $order): ?>
<tr>
<!-- ... existing table row data using $_order variable ... -->
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<div class="no-records">
<?php /* @escapeNotVerified */ echo __('No matching orders found') ?>
</div>
<?php endif; ?>
Now, the template will display only those orders that match the search criteria entered by the customer.
Conclusion
Implementing order search functionality in Magento requires a combination of modifying the default template, adding JavaScript for form handling, and updating the controller to filter orders based on the input parameters. By following these steps, you can enhance your customers’ experience, allowing them to quickly navigate to specific orders without scrolling through an extensive list.
At Laravel Company, we specialize in helping our clients optimize their e-commerce platforms for better user experiences and improved conversion rates. If you’re looking to add more advanced features or require custom development services, feel free to reach out to our team of expert Magento developers.