In this blog we’ll see how to add customer’s total order in the column customer grid in Magento 2. Customer grid in Magento 2 provides valuable insights into customer information, but it needs more visibility of a customer’s total order count.
First You Need to Create Custom module then follow this step. I have shared a step-by-step guide to add customer total order in the column customer grid.
Step 1: Create register.php
app/code/Bluethinkinc/Customergridordercount/registration.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethinkinc_Customergridordercount', __DIR__ ); ?> |
Step 2: Create module.xml
app/code/Bluethinkinc/Customergridordercount/etc/module.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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="Bluethinkinc_Customergridordercount" setup_version="1.0.0"> <sequence> <module name="Magento_Customer"/> </sequence> </module> </config> |
Step 3: Create customer_listing.xml
app/code/Bluethinkinc/Customergridordercount/view/adminhtml/ui_component/customer_listing.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?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="Bluethinkinc\Customergridordercount\Ui\Component\Listing\Column\OrdersCount" sortOrder="90"> <settings> <dataType>text</dataType> <label translate="true">Total Orders</label> <sortable>false</sortable> <filter>false</filter> </settings> </column> </columns> </listing> |
Step 4: Create OrdersCount.php
app/code/Bluethinkinc/Customergridordercount/Ui/Component/Listing/Column/OrdersCount.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<?php namespace Bluethinkinc\Customergridordercount\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 OrdersCount extends Column { protected $orderCollectionFactory; /** * @param ContextInterface $context * @param UiComponentFactory $uiComponentFactory * @param array $components * @param array $data */ public function __construct( ContextInterface $context, UiComponentFactory $uiComponentFactory, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, array $components = [], array $data = [] ) { $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: After create the module if you run the command as
php bin/magento module:status
You should see the module is disable now:
List of disabled modules: Bluethinkinc_ Customergridordercount
enable the module right now, let run the command as:
php bin/magento module:enable Bluethinkinc_ Customergridordercount
After Enable Module Then Run this Command
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento c:f
sudo chmod -R 777 var/ pub/static generated/
Now, you can login admin section go customer grid
Mukesh Singh
2024-01-02