2

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?

2
  • what is the response you are getting?
    – aravind
    Feb 28, 2020 at 4:49
  • @aravind way to get the product id without loading the entire entity Feb 28, 2020 at 15:11

1 Answer 1

0

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();

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.