0

I am using DI

  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory

in my class and get all products using

$collect = $this->collectionFactory->create()->getItems();

but when i need to filter specific category products using

$collect->addCategoryFilter($child_detail)

those products with VISIBILTY = Not Visible Individually do not appear in my search only VISIBILITY=Catalog, Search products appear.

kindly help thanks in advance.

5
  • that will create issue for user....@PrathapGunasekaran
    – Asad Ullah
    Commented Apr 5, 2019 at 13:08
  • but this is only way to all products by search and what do you mean by issue for users @AsadUllah Commented Apr 5, 2019 at 13:09
  • i mean when user look in search bar for a product let suppose "Erika Running Short" he will also get this products child in search bar like "Erika Running Short-32-Red" and other child products
    – Asad Ullah
    Commented Apr 5, 2019 at 13:13
  • and if he clicks on child product that URL doesn't exit that will create a problem
    – Asad Ullah
    Commented Apr 5, 2019 at 13:14
  • No the product won't go for 404 if the product VISIBILITY sets to search only in the sense, it will be visible only on search not on category listing. It doesn't mean that product URL won't exist and hope you get the point here Commented Apr 5, 2019 at 13:16

1 Answer 1

0

After digging on this particular question i have found if we use a custom query in our controller we can get all product including parent products and child-products all in one. Inject this to controller

 \Magento\Framework\App\ResourceConnection $resourceConnection,

Create object in __construct

$this->_resources = $resourceConnection;

In your Function use

$connection= $this->_resources->getConnection();
                     $select = $connection->select()
                         ->from(['o' =>  $this->_resources->getTableName('catalog_category_product')]);
                     $select->where('category_id = ?', 'add your category id' here);

                     $result = $connection->fetchAll($select);

We get Category products ids in $result and now for using Product_id separately use foreach and save

$proIds = array();
                     foreach ($result as $product)
                     {
                         $proIds[] = $product['product_id'];
                     }

Then inject \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory class and use it with $this->productCollectionFactory to filter your products from all products.

$collect = $this->productCollectionFactory->create()
                         ->addFieldToFilter('entity_id',array('in',$proIds));

Use foreach to deal with products separately

 foreach ($collect as $Product)
                     {
                         ***"Do what ever you want with product"***
                     }

If that helps hit like thanks

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.