I have some product SKUs in my custom module and I want to get just the simple products. I don't want to load each product entity by SKU in a loop to get the product type to avoid performance issues.
How could I filter these products?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI have some product SKUs in my custom module and I want to get just the simple products. I don't want to load each product entity by SKU in a loop to get the product type to avoid performance issues.
How could I filter these products?
You can use SearchCriteria to get load the product JUST if this product exists with the type that you want.
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product\Type;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\SearchCriteriaBuilder;
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('sku', $itemSku, 'eq')
->addFilter('type_id', Type::TYPE_SIMPLE, 'eq')
->create();
$productsItems = $this->productRepository->getList($searchCriteria)->getItems();