Question

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

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top