1

I am trying to get Child products from collection after applying category filter but unable to get all products (want to get all products including Not Visible Individually products

            $category = $this->categoryFactory->create()->load($value);
            $collection->addCategoryFilter($category);
            $ids = $collection->getAllIds();

Now in $ids i get only parent products i need all products parent and there child at same time.

Child and parent products all are enabled but can't access child

4
  • Can you describe, what you are trying to do? Aug 10 '20 at 14:08
  • i am just trying to get child product of group product after applying filter of a category to the product collection
    – Asad Ullah
    Aug 11 '20 at 12:19
  • is the attribute of the category is anchor set to yes or no ?
    – Marius
    Aug 12 '20 at 15:18
  • category anchor is set to yes
    – Asad Ullah
    Aug 12 '20 at 15:24
1
+50

Try following code:

protected $_productCollectionFactory;
  
public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
)
{    
    $this->_productCollectionFactory = $productCollectionFactory;
    parent::__construct($context);
}


public function getProductCollectionByCategories($ids)
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoriesFilter(['in' => $ids]);
    return $collection;
}

To get all product ids:

$collection = $this->getProductCollectionByCategories([$value]);
$ids = $collection->getAllIds();
2
  • Also you can refer link
    – Pratik Oza
    Aug 15 '20 at 7:18
  • thanks for the answer that's what i was looking for mate
    – Asad Ullah
    Aug 18 '20 at 15:08

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.