{"id":1619,"date":"2021-03-04T10:33:18","date_gmt":"2021-03-04T10:33:18","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-magento-2-collection-count\/"},"modified":"2025-07-16T12:40:46","modified_gmt":"2025-07-16T07:10:46","slug":"get-magento-2-collection-count","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-magento-2-collection-count\/","title":{"rendered":"How to Get Magento 2 Collection Count"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Collections in\u00a0Magento 2\u00a0are used to fetch multiple rows from tables, join tables with primary tables, select specific columns, apply a WHERE clause to query, or apply any conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A store owner may want to use collections to print order details with header and footer when the number of orders is greater than 0 and for no orders, i.e., 0 orders, display a message \u201cNo record found\u201d without a header and footer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In such scenarios, one may need to&nbsp;get Magento 2 collection count.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Return the total number of items from the collection, i.e., count the collection data using the below solution:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Magento 2 Collection Count<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here we are using an example of fetching all products and count these all fetched data.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory;\n\nprotected $productCollectionFactory;\n\npublic function __construct(CollectionFactory $productCollectionFactory)\n{\n    $this->productCollectionFactory = $productCollectionFactory;\n}\n\npublic function getProductCollection()\n{\n    $collection = $this->productCollectionFactory->create();\n    $collection->addAttributeToSelect('*');\n    return $collection;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We have got a whole collection of products using the above code, now call the getProductCollection() wherever you want to use the collection.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$collection = $this->getProductCollection();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now the main part, count the items collected in $collection by using the below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$collection->count();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can apply various validations, conditions, or anything according to your requirements using the collection count method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also remember when working with Magento 2 collections, it&#8217;s important to not only get the collection count but also manage the number of items per page <a href=\"https:\/\/meetanshi.com\/blog\/how-to-use-setpagesize-in-magento-2-collections\/\">using setPageSize in Magento 2<\/a> efficiently. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Collections in\u00a0Magento 2\u00a0are used to fetch multiple rows from tables, join tables with primary tables, select specific columns, apply a WHERE clause to query, or&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1619","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1619","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1619"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1619\/revisions"}],"predecessor-version":[{"id":15428,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1619\/revisions\/15428"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}