2

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?

5

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();
    }
}
  • Oh! does it mean i need to create this code? if i want to call the collection directly in my frontend file or block like magento 1? is it possible to call it? Is it standard method to do this? – jack Aug 12 '17 at 6:45
  • yes all are above standard. if you want to call it in template file use above code in block. add dependency and create function as show in answer and use that function in respected template file using $block->getProductCollection() – Prashant Valanda Aug 12 '17 at 6:50
  • In Product collection i will get entire product info ? How do i know about factory methods? – jack Aug 12 '17 at 6:56
  • Yes you will get whole product collection. for factory method refer devdocs.magento.com/guides/v2.0/extension-dev-guide/… – Prashant Valanda Aug 12 '17 at 7:04
  • Thanks for your help. how can i load category by product id? – jack Aug 12 '17 at 7:10
1

Magento 2 Product Collection:

In magento2, for getting product collection at any class,You should inject Factory class Magento\Catalog\Model\ResourceModel\Product\CollectionFactory in __construction function.

namespace [YourClassNameSpace];
class [YourClassName]
{
protected $_productCollectionFactory;
 public function __construct(
    ....
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    .....
    ) {
    .....
        $this->_productCollectionFactory = $productCollectionFactory;
    .....
 }

public function getProductCollection(){

    $collection = $this->_productCollectionFactory->create();
    /* Addtional */
    $collection
        ->addMinimalPrice() // add for Minimal
        ->addFinalPrice()  // add for Final Price 
        ->addTaxPercents() // add for Tax
        ->addAttributeToSelect('*') // use * for all product  attribute
        ->addUrlRewrite(); // Add for Url   
    return $collection;
}

}

Category collection:

For getting categry collection at any class,You should inject Factory class Magento\Catalog\Model\ResourceModel\Category\CollectionFactory in __construction function

namespace [YourClassNameSpace];
class [YourClassName]
{
    protected $_CategoryCollectionFactory;
     public function __construct(
        ....
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $CategoryCollectionFactory,
        .....
        ) {
        .....
        $this->_CategoryCollectionFactory = $CategoryCollectionFactory;
        .....
     }

    public function getCategoryCollection(){

        $collection = $this->_CategoryCollectionFactory->create();
        /* Addtional */
        $collection
            ->addAttributeToSelect('*') // use * for all Category  attribute
            ->joinUrlRewrite(); // Joins url rewrite rules to collection
        return $collection;
    }
}
  • Thanks for your reply, What's use of this code? $this->_productCollectionFactory = $productCollectionFactory; and in product collection why we need to do : $collection ->addMinimalPrice() // add for Minimal ->addFinalPrice() // add for Final Price ->addTaxPercents() // add for Tax ->addAttributeToSelect('*') // use * for all product attribute ->addUrlRewrite(); – jack Aug 12 '17 at 6:53
  • jack you need to understand the dependency injection. You that \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory --> $this->_productCollectionFactory = $productCollectionFactory; – Amit Bera Aug 12 '17 at 6:59
  • ok amitji. How can i load category from product id and load product by id? – jack Aug 12 '17 at 7:10

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.