How to add customer total order in the column customer grid in Magento 2?

How to add customer total order in the column customer grid in Magento 2?


The customer grid in Magento 2 provides valuable insights into customer information, but it needs more visibility of a customer's total order count. Adding the customer total order column to the grid lets you easily track and analyze customer behavior. In this blog, you will be guided through the necessary steps to achieve the desired customization.

Magento Customer Grid Customisation is very handy.

Follow the steps to customize the Customer Grid in Magento 2 and add the Customer's last logged-in date.

Step 1 :





Step 2 :



<?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="Elightwalk_CustomerGrid" setup_version="1.0.0">
           <module name="Magento_Customer"/>

Step 3 :



<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
   <columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns" >
       <column name="total_orders" class="Elightwalk\CustomerGrid\Ui\Component\Listing\Column\TotalOrders" sortOrder="90">
               <label translate="true">Total Orders</label>

Step 4 :



namespace Elightwalk\CustomerGrid\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class TotalOrders extends Column
   protected $orderCollectionFactory;
    * @param ContextInterface   $context           
    * @param UiComponentFactory $uiComponentFactory   
    * @param array              $components        
    * @param array              $data              
   public function __construct(
       ContextInterface $context,
       UiComponentFactory $uiComponentFactory,
       array $components = [],
       array $data = [],
       \Magento\Sales\Model\ResourceModel\Order\CollectionFactory  $orderCollectionFactory
   ) {

       $this->orderCollectionFactory = $orderCollectionFactory;
       parent::__construct($context, $uiComponentFactory, $components, $data);

    * Prepare Data Source
    * @param array $dataSource
    * @return array
   public function prepareDataSource(array $dataSource)
       if (isset($dataSource['data']['items'])) {
           foreach ($dataSource['data']['items'] as & $item) {
               $customerOrder = $this->orderCollectionFactory->create()->addFieldToFilter('customer_id', $item['entity_id']);
               $item[$this->getData('name')] = count($customerOrder);//Value which you want to display
       return $dataSource;

Step 5:


php bin/magento module:enable Elightwalk_CustomerGrid

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento cache:clean

Following the steps in this guide, you can improve the customer grid in Magento 2. This can be achieved by adding a column that displays each Customer's total number of orders. This customization provides valuable insights into customer behavior and helps you make data-driven decisions. Implement this feature to understand your customers better and improve your business strategies

For more Magento tips and tutorials, stay tuned to our blog or contact us for further assistance with your Magento customization needs.

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

Everything You Need to Know About Checkout/Cart Page Summary Definition of checkout/cart page

Uncover a thorough grasp of the Checkout/Cart Page summary. Dive into all you need to know about this e-commerce element, learning its definition and importance for a smooth online purchasing experience.

Read More
6 Essential Steps for Magento 2 Performance Optimization for Your Store (2024)

Accelerate your Magento 2 Performance Optimization in 2024! Follow these 6 steps for effective performance optimization. Elevate your website speed for improved customer satisfaction and SEO ranking.

Read More
Duplicate entry error When getting an order using REST API in Magento

When retrieving orders using the Magento REST API, troubleshoot the 'Duplicate Entry' problem. Our guide gives insights and solutions to ensure a seamless and error-free order retrieval experience on Magento platform.

Read More