Get product collection magento 2

It ‘s always better to move from Magento 1.x to Magento 2.x because it have too many cool features. Let’s talk about “get product, product collection in Magento 2″.  The coding style is better but different too. That ‘s why sometimes it take time to work with new Magento 2.x frameworks. Today we will talk about a very simple function in Magento 2 that getting product, product collection in magento 2.

Get product collection magento 2
Get product collection magento 2

Let’s try the checked solution what guarantee work well in magento 2.

Get Product Collection in Magento 2

Add this code in Factory

protected $_productCollectionFactory;
public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory
) {
        $this->_productCollectionFactory = $productFactory;
}
public function getProductCollection()
{ 
    return   $this->_productCollectionFactory->create()->addAttributeToSelect('*'); 
}

And then try to show product information in phtml file

$productCollection = $block->getProductCollection();
foreach ($productCollection as $product) {
   echo $product->getName().'
';
}

Now it ‘s done, you can focus to other logic :).
In this article, I want to try some other methods maybe you will love to use in some situation.

Get Category Collection in Magento 2

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;
    }

Get Product by ID in magento 2

protected $productRepository; 
  protected $_storeManager; 

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

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

Get 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();
    }
}

Quick load product from product id

In some case, you may want to make a quick load for a specific product ID, then you can use this way

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

If you debug a product you will see some fields like this

Array
(
[entity_id] => 1
[attribute_set_id] => 4
[type_id] => virtual
[sku] => This is sku
[has_options] => 0
[required_options] => 0
[created_at] => 2018-12-23 09:20:38
[updated_at] => 2018-12-23 09:20:38
[is_salable] => 1
)

Remain things are your turn, let’s try and see how it work.

References:

If you see the blank pages then maybe your core magento have problem with installation or wrong product id, category. In this case, you need to make sure the server/hosting you are working work well 100% with magento. You can check our Best Magento Hosting 2018.

Leave a Reply