Product By Category ID: How To Get Product Collection in Magento 2

This article is all about how we can get the collection of Product By Category ID. Wishusucess_ProductByCategory is a complete Magento 2 module to get the product collection on the CMS page.

In the ProductByCategory module, we have called the template files in cms_page_view.xml so when this action will get then the template will automatically call on the cms page and product collection with product URL will get load.

Get Product By Category ID

 

Get Product Collection By Category ID on CMS Page Basic File

So in order to create this module we have to create a minimum of five files.

app/code/Wishusucess/ProductByCategory/registration.php

app/code/Wishusucess/ProductByCategory/etc/module.xml

app/code/Wishusucess/ProductByCategory/Block/Categoryproduct.php

app/code/Wishusucess/ProductByCategory/view/frontend/layout/cms_page_view.xml

app/code/Wishusucess/ProductByCategory/view/frontend/templates/collection.phtml

 

Step 1: Registration File

app/code/Wishusucess/ProductByCategory/registration.php

<?php
/*
* @Author Hemant Singh
* @Developer Hemant Singh
* @Module Wishusucess_ProductByCategory
* @copyright Copyright (c) Wishusucess (http://www.wishusucess.com/)
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Wishusucess_ProductByCategory',
__DIR__
);

 

Step 2: Module XML File

app/code/Wishusucess/ProductByCategory/etc/module.xml

<?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="Wishusucess_ProductByCategory" setup_version="1.0.0" schema_version="1.0.0">
</module>
</config>

 

Create Block For Product Collection By Category ID in Magento 2

In order to get the product collection by CategoryID, We have to create a block Categoryproduct.php so all the Product Collection By Category ID will load.

app/code/Wishusucess/ProductByCategory/Block/Categoryproduct.php

<?php
/*
* @Author Hemant Singh
* @Developer Hemant Singh
* @Module Wishusucess_ProductByCategory
* @copyright Copyright (c) Wishusucess (http://www.wishusucess.com/)
*/
namespace Wishusucess\ProductByCategory\Block;

class Categoryproduct extends \Magento\Framework\View\Element\Template
{ 

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $_productCollectionFactory;

public function __construct(
\Magento\Backend\Block\Template\Context $context, 
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
)
{ 
$this->_productCollectionFactory = $productCollectionFactory;
parent::__construct($context);
}


public function getProductCollectionByCategories($ids)
{
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoriesFilter(['in' => ids]);
return $collection;
}
}

 

Call Template in CMS XML FIle

app/code/Wishusucess/ProductByCategory/view/frontend/layout/cms_page_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Wishusucess\ProductByCategory\Block\Categoryproduct" name="test" template="Wishusucess_ProductByCategory::collection.phtml"></block>
</referenceContainer>
</body>

</page>

 

Get Product Collection By Category ID

app/code/Wishusucess/ProductByCategory/view/frontend/templates/collection.phtml

<?php
/*
* @Author Hemant Singh
* @Developer Hemant Singh
* @Module Wishusucess_ProductByCategory
* @copyright Copyright (c) Wishusucess (http://www.wishusucess.com/)
*/
$ids = [98,164,165,99]; //Enter your cateogry id
$categoryProducts = $block->getProductCollectionByCategories($ids);
foreach ($categoryProducts as $product) {
echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

?>

 

Now we can use this collection template to call in the required page. for example, if you want to load product collection on the list page then simply you can call-template in the category list page through the following code.

<?php echo $this->getLayout()->createBlock("Wishusucess\ProductByCategory\Block\Categoryproduct")->setTemplate("Wishusucess_ProductByCategory::collection.phtml")->toHtml();?>

The above code we use to print the category collection on any kind of page.

 

Now, Run Following Command:

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deplpy -f

php bin/magento cache:clean

 

Other Option To Load Product Collection

<?php
/*
* @Author Hemant Singh
* @Developer Hemant Singh
* @Module Wishusucess_ProductByCategory
* @copyright Copyright (c) Wishusucess (http://www.wishusucess.com/)
*/
namespace Wishusucess\ProductByCategory\Block;

class Categoryproduct extends \Magento\Framework\View\Element\Template 
{

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/

protected $_productCollectionFactory;
protected $_categoryFactory;

public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Framework\View\Element\Template\Context $context
) {
$this->_categoryFactory = $categoryFactory;
$this->_productCollectionFactory = $productCollectionFactory;
}

public function getProductCollection()
{
$categoryId = 'yourcategoryid';
$category = $this->_categoryFactory->create()->load($categoryId);
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoryFilter($category);
$collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
return $collection;
}
}
?>

Download Link:

Wishusucess Product Collection By CategoryID in Magento 2

 

Hire Magento 2 Expert Developer to Develop Your Store

 

Related Post:

Custom Shipping Text Filed: Show Custom Text Magento 2

Search AutoComplete: Magento 2 Module Add All Category for Search

Override Checkout Street Address Placeholder in Magento 2

 

Recommended Post:

Magento 2.4 Installation Guide: How to Install Magento 2.4.2

Magento Store: Best 36 Magento Websites Example in The World

SEO Packages: How Much Do SEO Packages Cost in India, SEO Pricing

Leave a Reply