Is it possible to get a product collection bassd on array of product ids?
Magento2 Product Collection – How to Get Product Collection with Product IDs
magento2product-collection
magento2product-collection
Is it possible to get a product collection bassd on array of product ids?
Best Answer
Given an instantiated but not loaded collection
$collection
and an array of product ids$productIds
, you can useaddIdFilter()
just as in Magento 1:To instantiate a collection, you can inject a
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
and then useBut 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:
Magento\Catalog\Api\ProductRepositoryInterface
and\Magento\Framework\Api\SearchCriteriaBuilder
use Magento\Framework\Api\Filter;
Build a search criteria and pass it to
$productRepository->getList()
:$products
then is an array of products.