I want to get the list of products currently visible on category page in a custom block. I tried using

<?xml version="1.0"?>
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
                <block class="Magento\Catalog\Block\Product\ListProduct" name="mycustomblock" template="MyModule_Schema::product/customlist.phtml" before="-" />

But when I use this the sorting on category page does not work.

There are no logs except on system.log

You cannot define a correlation name 'review_summary' more than once [] []

I have also tried using this block using blockManager and also tried extending the block. Sorting does not work whenever I use this.

Is there any other way to get products without using that block?

  • Have you found the solution ? Apr 13 '20 at 7:24

Check below code

create a block to load product collection and pass data to a template.

File: Myvendor\Mymodule\Block\CatProductCollection.php;

 * @copyright DEVIMAGE
namespace Myvendor\Mymodule\Block;

use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\categoryFactory;
use Magento\Framework\View\Element\Template\Context;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Registry;
use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Framework\ObjectManagerInterface;

 * Class CatProductCollection
class CatProductCollection extends Template
    /** @var Registry  */
    private $_registry;

    /** @var categoryFactory  */
    private $_categoryFactory;

    /** @var CollectionFactory  */
    protected $_productCollectionFactory;

    /** @var Category  */
    protected $_category;

    /** @var **/
    protected $categoryRepository;

    protected $_objectManager = null;
     * CatProductCollection constructor.
     * @param Context $context
     * @param CollectionFactory $productCollectionFactory
     * @param Registry $registry
     * @param Category $category
     * @param categoryFactory $categoryFactory
     * @param array $data
    public function __construct(
        Context $context,
        CollectionFactory $productCollectionFactory,
        Registry $registry,
        Category $category,
        categoryFactory $categoryFactory,
        CategoryRepository $categoryRepository,
        ObjectManagerInterface $objectManager,
        array $data = []
    ) {
        parent::__construct($context, $data);

        $this->_registry = $registry;
        $this->_categoryFactory = $categoryFactory;
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_category = $category;
        $this->categoryRepository = $categoryRepository;
        $this->_objectManager = $objectManager;

     * @return array (product)
    public function getCategoryProductCollection()
        //get current category ID
        $category_load = $this->_registry->registry('current_category');
        $categoryId = $category_load->getId();
        //load product collection of category id wise
        $category_product_collection = $this->_categoryFactory->create()->load($categoryId);
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToFilter('visibility', Visibility::VISIBILITY_BOTH);

        return $collection;


Then, override catalog_category_view.xml in your custom module and add the below code in this layout.

<?xml version="1.0"?>
 * Copyright © 2020 DEVIMAGE. All rights reserved.
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <referenceContainer name="content">
            <block class="Myvendor\Mymodule\Block\CatProductCollection" name="catprocollection" template="Myvendor_Mymodule::customlist.phtml" />

Then show the collection in template file

 * @copyright DEVIMAGE
 * @var Myvendor\Mymodule\Block\CatProductCollection $block 

$catPCollection  = $block->getCategoryProductCollection();
foreach ($catPCollection as $product):
        echo $product->getId();

I hope this will help you.

  • This gives a collection of Products, not the products displayed on current page. Mar 24 '20 at 22:08
  • I have to get current category id and get all product collection and show in current page where you want me just print product id not all data
    – Devidas
    Mar 25 '20 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.