Displaying Magento Product List Block Inside a Custom Module
When developing custom modules for Magento, you may find that you need to display a filtered list of products within your module’s template. Fortunately, it is entirely possible to incorporate Magento’s product list block into your module without having to rebuild the layout and pagination functionality.
Using Magento’s Existing Product List Block
Magento provides built-in blocks like Magento\Catalog\Block\Product\ListProduct that handle the rendering of a products list with pagination. To use this block in your custom module, you can declare it as a dependency and inject it into your template file.
Here are the steps to follow:
- Declare the dependency in your module’s composer.json file:
"require": {
"magento/module-catalog": "~100.2"
}
- Create a block class in your module that extends
Magento\Catalog\Block\Product\ListProduct:
namespace YourCompany\YourModule\Block;
class ProductList extends \Magento\Catalog\Block\Product\ListProduct
{
// Add any additional functionality specific to your module here
}
- Register the block in your module’s
etc/di.xmlfile:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Block\Product\ListProduct">
<arguments>
<argument name="template" xsi:type="string">YourCompany_YourModule::product/list.phtml</argument>
</arguments>
</type>
</config>
- Create a template file
list.phtmlin your module’s view folder:
<!-- This is the content of list.phtml -->
<?= $block->getProductListHtml() ?>
- Include the necessary CSS and JavaScript files for the product listing:
You can use Magento’s built-in CSS and JavaScript files by including them in your layout XML file:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="Magento_Catalog::css/product/list.phtml"/>
<js src="Magento_Catalog::js/product/list.phtml"/>
</head>
</page>
Filtering Products in Your Module
To filter the products displayed by your custom module, you can use Magento’s built-in product collection functionality. You can create a new product collection and apply filters based on SKU, category, or other criteria.
For example, to retrieve a list of products with specific SKUs:
namespace YourCompany\YourModule\Model;
class ProductCollection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected $_eventPrefix = 'your_module_product_collection';
protected $_eventObject = 'product';
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init(\Magento\Catalog\Model\Product::class, \Magento\Catalog\Model\ResourceModel\Product::class);
}
public function addSkuFilter($sku)
{
if ($sku) {
$this->addFieldToFilter('sku', ['in' => explode(',', $sku)]);
}
}
}
You can then use this collection in your module’s template by injecting it as a dependency:
namespace YourCompany\YourModule\Controller;
class ProductList implements \Magento\Framework\App\ActionInterface
{
protected $collectionFactory;
//...
public function execute()
{
$sku = 'product-sku-1,product-sku-2';
$this->collectionFactory->create()->addSkuFilter($sku);
// Render your template with the filtered product collection
}
}
Conclusion
Incorporating Magento’s existing product list block into a custom module allows you to leverage the built-in functionality without having to recreate it from scratch. By following the steps outlined above, you can easily display a filtered set of products within your module while maintaining the native behavior of Magento’s product listing.
Remember that proper dependency management and template configuration are crucial for ensuring the seamless integration of Magento’s core blocks into your custom modules. Always test your implementation thoroughly to ensure it meets your specific requirements.
For more information on developing custom modules in Magento, refer to the official documentation at Laravel Company.