0

I want to show the specific products from array via list.phtml with pagination.

I referred https://github.com/BssGroup/HelloWorld and wrote the below code in block and controller.

$collection->addAttributeToFilter('entity_id', ['in' => $ids]);

But it gives all products when visiting http://localhost/helloworld/index/index page.

What is the best possible way to achieve this in Magento 2?

I am using Magento version 2.4.3-p1.

1
  • One thing I found by get_class() method is in the controller the collection is using Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor class. But in list.phtml its using Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\Interceptor class Mar 14, 2022 at 7:22

2 Answers 2

0

Have you tried clearing it first? Same on the code below:

<?php
namespace Bss\HelloWorld\Block\Index;

use Magento\Catalog\Block\Product\ListProduct;
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;

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

    public function getLoadedProductCollection()
    {
        $ids = [8360,13125];
        $collection = $this->_productCollection;
        $collection->clear();
        $collection->addAttributeToFilter('entity_id', ['in' => $ids]);
        return $collection;
    }
}
1
  • I tried your code but I found that the phtml is using different collection class. Please check my latest comment. Thanks Mar 14, 2022 at 7:23
0

I solved it by adding one condition to the elasticsearch. I found that one condition was coming from elasticsearch by echoing getSelect() method on the collection. And that one condition was creating the problem while adding filter.

For that I followed below answer:

https://magento.stackexchange.com/a/313477/28621

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.