Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to get product collection in Magento 2

Vinh Jacker | 12-18-2024

Get product collection

Table Content

Get Collection in Magento 2 means showing the items in your store when you run the command. With the code snippet in this topic, you can request the specific number of the product as you need. Let’s start calling the product in your Magento 2 store now!

Learn more: Get product collection by category ID in Magento 2

Overview of getting product collection in Magento 2

While there are different actions to get a certain product collection in Magento 2, they are loading, filtering (e.g. by attributes), and sorting. In this article, I will only introduce the fundamental steps to help you start working with product collection. Here are two initial steps:

  • Step 1: Declare in Mageplaza_HelloWorld Block
  • Step 2: Display product collection in phtml file

Step 1: Declare in Mageplaza_HelloWorld Block

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{    
    protected $_productCollectionFactory;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }
    
    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setPageSize(3); // fetching only 3 products
        return $collection;
    }
}
?>

Note: You can request the number of the product collection, which is a limited or unlimited number. While implementing this function, you will need to extend the code based on the requirement.

Step 2: Display product collection in phtml file

Print out the product collection in phtml file with the below code:

$productCollection = $block->getProductCollection();
foreach ($productCollection as $product) {
    print_r($product->getData());     
    echo "<br>";
}

Now, you can get the data of each product in the requested collection.

Hope that you can perform this function easily on your project. If you have any queries about getting product collections by filtering or sorting or any questions in general, use the comment section below!

Related Post

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services