How to work with product collection Magento 2

In this post we going to discuss about how to work with product collection in Magento 2 ,

Here the following collection are listed:

  • Obtain all product categories
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create ('Magento\Catalog\Model\ResourceModel\Product\Collection')

->addCategoryFilter($category);
  • Visibility filter
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

\Magento\Framework\App\ObjectManager::getInstance()

->create(Magento\Catalog\Model\Product\Visibility’)

->addVisibleInCatalogFilterToCollection($collection);
  • Status filter
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

\Magento\Framework\App\ObjectManager::getInstance()

->create(Magento\Catalog\Model\Product\Status’)

->addVisibleFilterToCollection($collection);
  • Insert product price to collection
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection')

  ->addFinalPrice()

->addTaxPercents();
  • Insert website ID to the collection
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection')

->addWebsiteNamesToResult();
  • Filter present store products
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection')

->addStoreFilter();
  • Filter present website products
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection')

  ->addWebsiteFilter();
  • Insert SEO product URL
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection')

->addUrlRewrite();
  • Insert category Ids
$collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection')

  ->addCategoryIds();
  •  Insert tier pricing
 $collection = \Magento\Framework\App\ObjectManager::getInstance()

->create('Magento\Catalog\Model\ResourceModel\Product\Collection')

->addTierPriceData();

The following steps I mentioned above is the smallest process for you to Work with product collections in Magento 2. With this guidelines you can manage the product collection in Magento 2 simply, all stores has a n product collection in Magento 2 in Magento 2 with multiple elements.

Last Update: April 9, 2018  

February 14, 2018   191   Nandini R    Tips And Tricks    
Total 3 Votes:
1

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Google+
http://www.webnexs.com/blog/kb/to-work-product-collection-magento-2
Twitter
LinkedIn