1

How to do product listing on custom module page?

I have tried but it prints array.

3
1

In your block Class add product collection factory to you constructor

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
) {
    $this->productCollectionFactory = $productCollectionFactory;
}

Add getProductCollection method

public function getProductCollection()
{
    $collection = $this->productCollectionFactory->create();
    return $collection;
}

If you want to filter products you can use addAttributeToFilter method.

$collection->addAttributeToFilter('yourAttributeName', 'YourAttributeValue']);

Then in your template file call getProductCollection method and iterates on returned object:

<?php $_products = $block->getProductCollection()->getItems(); ?>
<?php foreach ($_products as $_product): ?>
    <h1> <?php echo $_product->getName(); ?> </h1>
<?php endforeach; ?>

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