<h1>Customer List</h1>
<?php //echo $this->getCustomerCollection(); ?>
<?php //var_dump($this->getCustomersCollection());
$customerCollection = $this->getCustomerCollection();
foreach ($customerCollection as $customer) {
echo 'Customer Name: '.$customer->getName().'<br/>';
echo 'Customer Email: '.$customer->getEmail().'<br/>';
echo '==============================================<br/>';
}
?>
<h1>Customer List with the name "Test"</h1>
<?php //echo $this->getFilteredCustomerCollection();
$customerCollection = $this->getFilteredCustomerCollection();
foreach ($customerCollection as $customer) {
echo 'Customer Name: '.$customer->getName().'<br/>';
echo 'Customer Email: '.$customer->getEmail().'<br/>';
echo '==============================================<br/>';
}
?>
Step 6 : Create a layout file:
If we want to call a phtml file on a page then we need to create a layout file to achieve this. As a next move, let us create a folder inside frontend folder and name it as layout and inside this layout folder, create an xml file and
name it as customercollection_customer_index
app/code/Code5fixer/CustomerCollection/view/frontend/layout/customercollection_customer_index.xml
Here, route id (specified in routes.xml)_name of the folder inside controller folder_action class must be the name of this layout file.
The contents of this layout file will be:
customercollection_customer_index
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd" layout="1column">
<body>
<referenceContainer name="content">
<block class="Code5fixer\CustomerCollection\Block\CustomerCollection" name="customercollection" template="customercollection.phtml" />
</referenceContainer>
</body>
</page>
Step 7 : Run all the necessary commands
We need to run commands like setup:upgrade, static-content:deploy, setup:di:compile, reindex and cache:flush from the Magento root directory.
To know more about more magento commands, refer this blog.
Let us open the below URL in our browser
http://yourhost.com/customercollection/customer/index
And now, our browser will be as shown below.
We hope, with the help of the above steps, you will be easily getting the customer collection in Magento2. Also, share this blog with your developer friends.
You could also download the above module here
After downloading this module:
-
Extract the downloaded zip file
-
Paste the extracted Code5fixer folder into /app/code/ folder in your magento root directory.
-
Run all the necessary 5 commands and check for output.
Note: This module is developed using Magento ver. 2.4.3-p1 version. But if you try this in older or newer versions, it may give some errors.
You could also refer to this demo video for better understanding.
We hope our guide would be very useful for you. If you have any questions, feel free to reach us anytime by leaving a comment below.
Check out our new blogs here
Comments