2

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? – Chintan Kaneriya Nov 17 '18 at 4:36
1

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

<?php
namespace Vendor\Extension\Block;

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

    protected $stockFilter;

    public function __construct(
        \Magento\Backend\Block\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();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

        // ADD THIS CODE IF YOU WANT IN-STOCK-PRODUCT
        $this->stockFilter->addInStockFilterToCollection($collection);

        return $collection;
    }
}
?>

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

| improve this answer | |
  • Sorry Dhiren, I have checked it already. I just trying to apply all filters in single collection. – Suman Singh Feb 17 '18 at 13:08
  • you need to add $collection->addAttributeToFilter in same code if you want some more filter with this. – Dhiren Vasoya Feb 17 '18 at 13:09
  • For instock/outstock in same collection? – Suman Singh Feb 17 '18 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. – Dhiren Vasoya Feb 17 '18 at 13:14
  • Please let me know how to use addAttributeToFilter for instock and out of stock products in same collection? – Suman Singh Feb 17 '18 at 13:17
0

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:

<?php 

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/

| improve this answer | |
  • don not want to use the loops to manipulate collection. Is it possible with single collection and filters? – Suman Singh Feb 17 '18 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? – Ethan Yehuda Feb 17 '18 at 14:58
0

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

app/design/frontend/your_vendor/your_theme/Magento_Catalog/templates/product/list.phtml

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

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

$collection = $productCollection->create()

 ->addAttributeToSelect('*')

 ->load();
| improve this answer | |

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.