How to show custom collection as the normal product listing?

Created by Support Agent, Modified on Sun, 19 Apr at 10:53 PM by Support Agent

Question:

I want to show my custom collection( means only showing products whose manufacture is "Dell") as the normal product listing in this module: https://github.com/BssGroup/HelloWorld  


Can you please let me know how to achieve that? 


Answer:

Regarding your query, we have checked and would like to give you the guide to filter attribute product.
In your case, the attribute manufacture is "Dell"
Please follow these steps:

Step 1: Go to the admin to inspect attribute manufacture and get value filter 

Step 2: Edit this file app/code/Bss/HelloWorld/Block/Index/Collection.php

<?php
namespace Bss\HelloWorld\Block\Index;
use Magento\Catalog\Block\Product\ListProduct;
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
use Magento\Catalog\Model\ResourceModel\Product\Collection as Collections;
use Magento\Catalog\Block\Product\ProductList\Toolbar;
class Collection extends ListProduct {



    public function setProductCollection(AbstractCollection $collection)
    {
        $this->_productCollection = $collection;
    }

    public function getLoadedProductCollection()
    {
        return $this->_productCollection;
    }

    protected function _getProductCollection()
    {
        if ($this->_productCollection === null) { 
            $this->_productCollection = $this->initializeProductCollection();
        }

        return $this->_productCollection;
    }

    private function addToolbarBlock(Collections $collection)
    {
        $toolbarLayout = $this->getToolbarFromLayout();

        if ($toolbarLayout) {
            $this->configureToolbar($toolbarLayout, $collection);
        }
    }

    private function getToolbarFromLayout()
    {
        $blockName = $this->getToolbarBlockName();

        $toolbarLayout = false;

        if ($blockName) {
            $toolbarLayout = $this->getLayout()->getBlock($blockName);
        }

        return $toolbarLayout;
    }

    private function configureToolbar(Toolbar $toolbar, Collections $collection)
    {
        // use sortable parameters
        $orders = $this->getAvailableOrders();
        if ($orders) {
            $toolbar->setAvailableOrders($orders);
        }
        $sort = $this->getSortBy();
        if ($sort) {
            $toolbar->setDefaultOrder($sort);
        }
        $dir = $this->getDefaultDirection();
        if ($dir) {
            $toolbar->setDefaultDirection($dir);
        }
        $modes = $this->getModes();
        if ($modes) {
            $toolbar->setModes($modes);
        }
        // set collection to toolbar and apply sort
        $toolbar->setCollection($collection);
        $this->setChild('toolbar', $toolbar);
    }


    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(current($categories->getIterator())->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();
        $collection->addAttributeToFilter('manufacturer','5640');

        $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

        if ($origCategory) {
            $layer->setCurrentCategory($origCategory);
        }

        $this->addToolbarBlock($collection);

        $this->_eventManager->dispatch(
            'catalog_block_product_list_collection',
            ['collection' => $collection]
        );

        return $collection;
    }

}

Note: Check and edit this code with the corresponding value on your site

$collection->addAttributeToFilter('manufacturer','5640');

Step 3: Flush the cache and check the result  

For any further support, please don't hesitate to contact us at support@bsscommerce.com

We're always willing to support. If you need further our information, here is:


BSS Commerce is a trusted Magento 2 extensions provider with 14+ years of experience, serving 75,000+ customers worldwide. We are an Adobe Commerce Solution Bronze Partner and a Select Magento Extensions Builder, reinforcing our expertise in the Magento ecosystem.


We specialize in developing 150+ high-quality Magento 2 extensions that enhance functionality, improve user experience, and support scalable growth. Our reputation is built on long-term partnerships, positive feedback, and proven performance-driven solutions.


Visit us to get more useful plugins: https://bsscommerce.com/magento-2-extensions.html 


Here to discover our extensions:

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article