11

Is it possible to get a product collection bassd on array of product ids?

deroccha
  • 687
  • 2
  • 9
  • 21

3 Answers3

22

Given an instantiated but not loaded collection $collection and an array of product ids $productIds, you can use addIdFilter() just as in Magento 1:

$collection->addIdFilter($productIds);

To instantiate a collection, you can inject a \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory and then use

$collection = $this->collectionFactory->create();

But this is not recommended practice anymore!


In Magento 2, you should not think too much in terms of collections anymore when using core modules, they are a mere implementation detail. Use the service contracts instead:

  • Inject Magento\Catalog\Api\ProductRepositoryInterface and \Magento\Framework\Api\SearchCriteriaBuilder
  • use Magento\Framework\Api\Filter;
  • Build a search criteria and pass it to $productRepository->getList():

    $searchCriteria = $this->searchCriteriaBuilder->addFilter(new Filter([
        Filter::KEY_FIELD => 'entity_id',
        Filter::KEY_CONDITION_TYPE => 'in',
        Filter::KEY_VALUE => $productIds
    ]))->create();
    $products = $this->productRepository->getList($searchCriteria)->getItems();
    

    $products then is an array of products.

Fabian Schmengler
  • 65,791
  • 25
  • 187
  • 421
  • 1
    Maybe I'm missing something but using repositories doesn't seem to allow full control on the result; for example: how do you add an attribute to EAV entities? Or join an external table? In that case I still see Magento2 core uses collections. Is there a valid alternative? Thank you! – Alessandro Ronchi Jun 07 '16 at 08:01
  • 1
    Unfortunately there is not. There are often cases where you still need collections. See also: https://magento.stackexchange.com/a/158098/243 – Fabian Schmengler Jul 19 '17 at 11:36
  • Exactly my problem now. I built a working method to get a result with repository and the result could not be modified (or even put into a collection neatly, missing add/set data methods... and many other issues). – LM_Fielding Aug 11 '17 at 09:48
11

Use SearchCriteria and Product Repositories:

$productIds = [.....];
$searchCriteria = $this->searchCriteriaBuilder
                ->addFilter('entity_id', $productIds, 'in')
                ->create();

$products = $this->productRepositoryInterface->getList($searchCriteria)->getItems();

To get search criteria builder and product repository object you have to require:

  • Magento\Framework\Api\SearchCriteriaBuilder
  • Magento\Catalog\Api\ProductRepositoryInterface
Phoenix128_RiccardoT
  • 7,065
  • 2
  • 22
  • 36
1
$porductIds=array(2,6,7);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
    $collectionByIds = $productCollection->addAttributeToSelect('*');
    $collectionByIds->addFieldToFilter('entity_id', array('in' => $data));
    $collectionByIds->load();

    foreach ($collectionByIds as $collection) :
        echo "<pre>";
        print_r($collection->getName());
    endforeach;
Baharuni Asif
  • 1,217
  • 9
  • 26