4.5 (90.82%) 85 votes

Get All Products of a category

$collection = \Magento\Framework\App\ObjectManager::getInstance() ->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addCategoryFilter($category); 1 2 3 4 5 $ 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); 1 2 3 4 5 6 7 8 9 $ 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); 1 2 3 4 5 6 7 8 9 $ 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 ) ;

——————————————

Add Product Price To Collection

$collection = \Magento\Framework\App\ObjectManager::getInstance() ->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addFinalPrice() ->addTaxPercents(); 1 2 3 4 5 6 7 $ collection = \ Magento \ Framework \ App \ ObjectManager :: getInstance ( ) -> create ( 'Magento\Catalog\Model\ResourceModel\Product\Collection' ) -> addFinalPrice ( ) -> addTaxPercents ( ) ;

—————————————

Add Website ID to the collection

$collection = \Magento\Framework\App\ObjectManager::getInstance() ->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addWebsiteNamesToResult(); 1 2 3 4 5 $ collection = \ Magento \ Framework \ App \ ObjectManager :: getInstance ( ) -> create ( 'Magento\Catalog\Model\ResourceModel\Product\Collection' ) -> addWebsiteNamesToResult ( ) ;

—————————————

Filter Current Store Products

$collection = \Magento\Framework\App\ObjectManager::getInstance() ->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addStoreFilter(); 1 2 3 4 5 $ collection = \ Magento \ Framework \ App \ ObjectManager :: getInstance ( ) -> create ( 'Magento\Catalog\Model\ResourceModel\Product\Collection' ) -> addStoreFilter ( ) ;

——————————

Filter Current Website Products

$collection = \Magento\Framework\App\ObjectManager::getInstance() ->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addWebsiteFilter(); 1 2 3 4 5 $ collection = \ Magento \ Framework \ App \ ObjectManager :: getInstance ( ) -> create ( 'Magento\Catalog\Model\ResourceModel\Product\Collection' ) -> addWebsiteFilter ( ) ;

——————————

Add SEO Product URL

$collection = \Magento\Framework\App\ObjectManager::getInstance() ->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addUrlRewrite(); 1 2 3 4 5 $ collection = \ Magento \ Framework \ App \ ObjectManager :: getInstance ( ) -> create ( 'Magento\Catalog\Model\ResourceModel\Product\Collection' ) -> addUrlRewrite ( ) ;

————————

Add Category Ids

$collection = \Magento\Framework\App\ObjectManager::getInstance() ->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addCategoryIds(); 1 2 3 4 5 $ collection = \ Magento \ Framework \ App \ ObjectManager :: getInstance ( ) -> create ( 'Magento\Catalog\Model\ResourceModel\Product\Collection' ) -> addCategoryIds ( ) ;

————————

Add Tier Pricing

$collection = \Magento\Framework\App\ObjectManager::getInstance() ->create('Magento\Catalog\Model\ResourceModel\Product\Collection') ->addTierPriceData(); 1 2 3 4 5 $ collection = \ Magento \ Framework \ App \ ObjectManager :: getInstance ( ) -> create ( 'Magento\Catalog\Model\ResourceModel\Product\Collection' ) -> addTierPriceData ( ) ;

The steps I mention above is the shortest process for you to Work with Product Collection in Magento 2. With this guide, you can manage the Product Collection in Magento 2 easily. Every store has an Product Collection in Magento 2 in Magento 2 with many attributes.

Thank you for reading this post and see you in other posts from Magestore!