Magento 2.1 – How to Load Product Collection, Category Collection, Load Product by ID, and Get Category by Product ID

categorymagento-2.1product

I do have experience with magento 1 and in magento 1 i do know how to load the product collection, category collection, quote and product load by id.

Magento 2 is really new concept for me and syntax is completely changed in it. Need help how can i load Product collection, category collection, quote and product by id using magento 2 standard methods.

Magento 1 Product Collection:

 $productCollection = Mage::getModel('catalog/product')->getCollection();

How to call product collection in magento 2.

Magento 1 Category Collection:

$categoryCollection = Mage::getModel('catalog/category')->getCollection();

How to call category collection in magento 2

Magento 1 Load product by id :

$product = Mage::getModel('catalog/product')->load($id);

How to load in magento 2

Magento 1 load category by product id:

    $product = Mage::getModel('catalog/product')->load($_item['product_id']);
    $cats = $product->getCategoryIds();

How to load in magento 2?

Best Answer

CALL PRODUCT COLLECTION

You need to define dependency as below

public function __construct(    
    ....
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    ...
)
{    
    $this->_productCollectionFactory = $productCollectionFactory;
}

 public function getProductCollection()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    return $collection;
}

For Category collection

   protected $_categoryCollection;

   public function __construct(
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection       
    ) {
        $this->_categoryCollection = $categoryCollection;
    }      

    public function getCategoryCollection()
    {
        $collection = $this->_categoryCollection->create()
            ->addAttributeToSelect('*');

        return $collection;
    }

Product Get By ID

  protected $productRepository; 
  protected $_storeManager; 

  public function __construct(
    ProductRepositoryInterface $productRepository
  ) {
      $this->productRepository = $productRepository;
  }
  public function getProduct()
  {

      $productId=1;
      return $product = $this->productRepository->getById($productId);
  }

You can also do same for load category by product id.

Category By product ID

protected $_categoryFactory
protected $_productFactory

public function __construct(    
    ....
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Catalog\Model\ResourceModel\CategoryFactory $categoryFactory 
    ...
){
    $this->_categoryFactory = $categoryFactory;
    $this->_productFactory = $productFactory;

}

public function getCategoryFromProductId(){
    $product = $this->_productFactory->create()->load($pid);
    $cats = $product->getCategoryIds(); //array
    if(count($cats) ){
        $firstCategoryId = $cats[0];
        $_category = $this->_categoryFactory->create()->load($firstCategoryId);
        return $_category->getName();
    }
}