Skip to content

Help in finding the code responsible for fetching the products on Category Pages

This might feel a lengthy question but actually it is not big. I have just added some functions that I followed to try finding the actual code responsible for loading the product collection on category pages.

I am trying to look for the code that is responsible for fetching the products on category pages.
These are the steps that I have followed.

  1. Checked the list.phtml file that displays the HTML for category pages. There I found the following code.
    File path for list.phtml
    vendor/magento/module-catalog/view/frontend/templates/product/list.phtml

enter image description here

  1. Then I went to the following block “@var $block MagentoCatalogBlockProductListProduct”
    and checked the getLoadedProductCollection function. This function took me to the following function “initializeProductCollection”. Content of initializeProductCollection function.

    private function initializeProductCollection()
     {
     $layer = $this->getLayer();
     /* @var $layer Layer */
     if ($this->getShowRootCategory()) {
         $this->setCategoryId($this->_storeManager->getStore()->getRootCategoryId());
     }
    
     // if this is a product view page
     if ($this->_coreRegistry->registry('product')) {
         // get collection of categories this product is associated with
         $categories = $this->_coreRegistry->registry('product')
             ->getCategoryCollection()->setPage(1, 1)
             ->load();
         // if the product is associated with any category
         if ($categories->count()) {
             // show products from this category
             $this->setCategoryId($categories->getIterator()->current()->getId());
         }
     }
    
     $origCategory = null;
     if ($this->getCategoryId()) {
         try {
             $category = $this->categoryRepository->get($this->getCategoryId());
    
         } catch (NoSuchEntityException $e) {
             $category = null;
         }
    
         if ($category) {
             $origCategory = $layer->getCurrentCategory();
             $layer->setCurrentCategory($category);
         }
     }
     $collection = $layer->getProductCollection();
     $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());
    
     if ($origCategory) {
         $layer->setCurrentCategory($origCategory);
     }
    
     $this->_eventManager->dispatch(
         'catalog_block_product_list_collection',
         ['collection' => $collection]
     );
    
      return $collection;
     }
    

This function has the following line that is loading the product collection.

$collection = $layer->getProductCollection();
  1. I looked and found out that $layer is calling get function of the following class – MagentoCatalogModelLayerResolver

This is the content of the get function

/**
     * Get current Catalog Layer
     *
     * @return MagentoCatalogModelLayer
     */
    public function get()
    {
        if (!isset($this->layer)) {
            $this->layer = $this->objectManager->create($this-layersPool[self::CATALOG_LAYER_CATEGORY]);
    }
    
    return $this->layer;
}

If you see this function then it is creating this class MagentoCatalogModelLayerCategory

  1. So I went to this class but did not found anything but it has a parent class

     class Category extends MagentoCatalogModelLayer
    
  2. Now I went to the parent class (MagentoCatalogModelLayer) and found the following function

    public function getProductCollection()
     {
         if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
             $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
         } else {
             $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
             $this->prepareProductCollection($collection);
             $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
         }
    
         return $collection;
     }
    
  3. There I saw collection is called in the following line.

     $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());
    

I found out that $this->collectionProvider has following value assigned.

    $this->collectionProvider = $context->getCollectionProvider();

    And $context is this, MagentoCatalogModelLayerContextInterface $context,
  1. So now I went to the MagentoCatalogModelLayerContextInterface class to find the getCollection function that I saw in this line:
    $collection = $this->collectionProvider->getCollection($this->getCurrentCategory());

  2. Now ContextInterface class has only the definition for this function and not the entire code

enter image description here

  1. But I found out that it is returning @return ItemCollectionProviderInterface so I went to this class and it has the following code.

enter image description here

  1. Here also we have the definition for getCollection function and not the entire code.
    But then again this function is returning @return MagentoCatalogModelResourceModelProductCollection

  2. So at last I reached to the Resource Model Class but again in this class I did not find getCollection function.

  3. I know ResourceModel are used by Magento for database related things. But here I could not find any code that is actually returning the products collection based on the category.

Could someone help me in understanding this and let me know how and where is the code that is responsible for fetching the products on category pages.
I am really stuck into this and do not where to proceed now from here. Some guidance is really appreciated.