In the class Magento\Catalog\Block\Product\ListProduct, how can we modify private method "initializeProductCollection()" without changing anything in this core file?

Please help!!!

  • You need to override it in your custom module. Commented Sep 12, 2018 at 7:56
  • This is private method.we can't override. Commented Sep 12, 2018 at 7:59
  • @SukumarGorai, can you pls update the code, how to override the category loaded product collection? Commented Oct 14, 2019 at 6:04

2 Answers 2


@wakar Yes , we can not override private method but we can override all the methods of that class which is public and using that private method .

we need to usually override all methods that call the private method you change and replace the call .

for example

initializeProductCollection() has been called under _getProductCollection() which is protected and that function is called under getLoadedProductCollection() which is public so override that function/method . and create your own method _MygetProductCollection and MyinitializeProductCollection() and make the changes you want to make .

something like this will help , I have not tested the code

di.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <preference for="Magento\Catalog\Block\Product\ListProduct" type="Vendor\Module\Block\Product\ListProduct" />

your class file at location Vendor\Module\Block\Product\ListProduct.php

    class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
        public function MyinitializeProductCollection(){


        public function  _MygetProductCollection(){
    // make changes here means call MyinitializeProductCollection in place of initializeProductCollection

        public function getLoadedProductCollection(){
            // make changes here means call _MygetProductCollection in place of _getProductCollection

hope this helps .

  • This is not working for me.. Commented Oct 14, 2019 at 8:01
  • @jafarpinjar are you getting any error?
    – Mac
    Commented Oct 24, 2019 at 6:26
  • the code seems not fully given, pls update complete code Commented Feb 25, 2020 at 7:04

Here is the code, for reference here, before return of collection we have just added complete category collection through Category Factory.

namespace Vendor\ModuleName\Block\Rewrite\Product;
use Magento\Catalog\Model\Product;

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
    protected $_customerSession;
    protected $categoryFactory;

    public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
    \Magento\Catalog\Model\Layer\Resolver $layerResolver,
    \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    array $data = [],
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
    ) {
        $this->_customerSession = $customerSession;
        $this->categoryFactory = $categoryFactory;
        $this->_helper = $helper;


    private function initializeProductCollection()
        $layer = $this->getLayer();
        /* @var $layer Layer */
        if ($this->getShowRootCategory()) {

        // 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)
            // if the product is associated with any category
            if ($categories->count()) {
                // show products from this category

        $origCategory = null;
        if ($this->getCategoryId()) {
            try {
                $category = $this->categoryRepository->get($this->getCategoryId());
            } catch (NoSuchEntityException $e) {
                $category = null;

            if ($category) {
                $origCategory = $layer->getCurrentCategory();
            $categoryId = $this->getLayer()->getCurrentCategory()->getId();

            $category =  $this->categoryFactory->load($categoryId);
            $collection = $category->getProductCollection()
        // $collection = $layer->getProductCollection();

        if ($origCategory) {

            ['collection' => $collection]

        return $collection;


P.S-Please correct i am wrong as i am newbie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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