I need to get all simple products of a category. By using the following code, I'm getting only products which has visibility of Catalog, Search but not Not Visible Individually products.

$categoryId = 12;
$categoryFactory = $objectManager->create('Magento\Catalog\Model\CategoryFactory');
$category = $categoryFactory->create()
    ->addAttributeToFilter('type_id', 'simple');

I need all simple products regardless of its visibility. How can I achieve this?


Try This :-

$categoryId = '12';
$productCollection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$categoryCollection = $objectManager->create('\Magento\Catalog\Model\CategoryFactory');

    $category = $categoryCollection->create()->load($categoryId);
    $collection = $productCollection->create()
                    ->addAttributeToFilter('visibility', array('eq' => array(id1,id2))); //you can pass id as per your requirement

Reference Link :-

Get product collection by category id on phtml file magento2

  • I said I need both "Catalog, Search" and "Not Visible Individually" simple products. But you are applying filter to get only "Catalog, Search" products. – Vinoth S Apr 5 at 14:45
  • check updated answer – Rk Rathod Apr 5 at 17:21
->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);

Try this one, may help you

  • No change in result. Getting on visible products. – Vinoth S Apr 5 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.