I am trying to fetch the product collection in Magento 2 which return the all products including

-> with all instock and out of stock products

-> with all Visibility (Not Visible Individually,Catalog,Search,Catalog, Search)

-> with all status (Enabled,Disabled)

Is it possible to get this type of product collection?

  • where do you want to show this collection? Nov 17, 2018 at 4:36

3 Answers 3


Using bellow code, you fetch the product which is currently enable.

namespace Vendor\Extension\Block;

class Yourblock extends \Magento\Framework\View\Element\Template
    /*Product collection variable*/ 
    protected $_productCollection;

    protected $stockFilter;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
        \Magento\CatalogInventory\Helper\Stock $stockFilter,        
        array $data = []
        $this->_productCollection= $productCollection;
        $this->stockFilter = $stockFilter;    
        parent::__construct($context, $data);
    public function getProductCollection()

        $collection = $this->_productCollection->create();


        return $collection;

NOTE You need to change getProductCollection() function code for your other requirement.

  • Sorry Dhiren, I have checked it already. I just trying to apply all filters in single collection. Feb 17, 2018 at 13:08
  • you need to add $collection->addAttributeToFilter in same code if you want some more filter with this. Feb 17, 2018 at 13:09
  • For instock/outstock in same collection? Feb 17, 2018 at 13:12
  • you can add "addAttributeToFilter" as many as you want in same collection logic. And if my answer help you, you can accept so it will help other in community. Feb 17, 2018 at 13:14
  • Please let me know how to use addAttributeToFilter for instock and out of stock products in same collection? Feb 17, 2018 at 13:17

This might be a duplicate of: https://stackoverflow.com/questions/34740555/how-to-get-model-and-product-collection-in-magento-2/35679085#35679085

But here's an example anyway:


use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class ProductCollectionExample
     * Product collection
     * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
    protected $collection;

     * Construct
     * @param CollectionFactory $collectionFactory
    public function __construct(
        CollectionFactory $collectionFactory,
    ) { 
        $this->collection = $collectionFactory->create();

    public function doStuff()
        foreach($this->collection as $product) {
            // do stuff

Another option is that you can use the Magento\Catalog\Model\ProductRepository and leverage the getItems() method.

It's a bit more complicated IMHO, but it basically abstracts collections another level higher (little bit of a over simplification).

Take a look at this article by Alan Storm: https://alanstorm.com/magento_2_understanding_object_repositories/

  • don not want to use the loops to manipulate collection. Is it possible with single collection and filters? Feb 17, 2018 at 6:17
  • Can you explain what you mean by "single collection and filters?" You can use either addAttributeToFilter() if it's an EAV model, or addFieldToFilter() otherwise. I believe by default, it emulates a SELECT * so I'm not sure what you're trying to do. You could load it into an array also with $collection->toArray(). Is that what you mean by single collection? Feb 17, 2018 at 14:58

If you want to display your collection in listing page. then add below code in List.phtml file


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

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

$collection = $productCollection->create()



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.