2

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()
    ->load($categoryId)
    ->getProductCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('type_id', 'simple');

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

2 Answers 2

1

Try This :-

Visibility Ids

VISIBILITY_NOT_VISIBLE = 1;

VISIBILITY_IN_CATALOG = 2;

VISIBILITY_IN_SEARCH = 3;

VISIBILITY_BOTH = 4;

$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()
                    ->addAttributeToSelect('*');
                    ->addCategoryFilter($category);
                    ->addAttributeToFilter('visibility', array('eq' => array(2,3))); //you can pass id as per your requirement
6
  • I said I need both "Catalog, Search" and "Not Visible Individually" simple products. But you are applying filter to get only "Catalog, Search" products. Apr 5, 2019 at 14:45
  • check updated answer Apr 5, 2019 at 17:21
  • @VinothSelvi have you gotten both "Catalog, Search" and "Not Visible Individually" simple products. i want to get both but can't Oct 22, 2021 at 12:25
  • @HafizArslan yes I got all simple products using the above solution. Oct 25, 2021 at 6:44
  • please attach your code @Hafiz Arslan Oct 25, 2021 at 10:25
0
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);

Try this one, may help you

1
  • No change in result. Getting on visible products. Apr 5, 2019 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.