19

I want to use functions like getName(), getPrice(), etc. of product to retrieve product information in Magento 2.

Can anyone show me how can I achieve that ?

1
  • @JJohn, Is it working below answers. If Yes accept it will useful for other users. If no let me know, so that we can provide solution.
    – Bojjaiah
    Nov 30, 2015 at 6:35

6 Answers 6

22

In Magento 2 proposed to use service layer for this. Try use \Magento\Catalog\Model\ProductRepository::getById method to get product by id

3
  • 2
    You should use \Magento\Catalog\Api\ProductRepositoryInterface::getById instead. Feb 11, 2018 at 16:38
  • its not recommended to use ProductRepository in Magneto 2
    – Waqar Ali
    Jul 2, 2019 at 10:30
  • @WaqarAli, then what is recommended?
    – Black
    Jan 9, 2020 at 16:13
14

On top of others answers, I highly suggest using the following service contracts methods:

  • \Magento\Catalog\Api\ProductRepositoryInterface::getById : to load a product by id
  • \Magento\Catalog\Api\ProductRepositoryInterface::get : to load a product by sku
1
12

Magento 2 recommends to use Factories for loading all Models. Here is how you should do it:

  1. Above your class add this line:

    use Magento\Catalog\Model\ProductFactory;
    
  2. Now create class property:

    protected $productFactory;
    
  3. In your constructor, add dependency:

    public function __construct(
       ProductFactory $productFactory
    ) {
       $this->productFactory = $productFactory;
    }
    
  4. Now load product as below:

    $_product = $this->productFactory->create()->load(<product_id>);
    
3
  • there is no class \Magento\Catalog\Model\ProductFactory in Magento 2.1.7 Sep 10, 2017 at 16:44
  • 2
    Most factory classes don't exist in the module. They are generated in var/generation to instantiate classes using the object manager. You should not use factories to load products anyway. Use \Magento\Catalog\Api\ProductRepositoryInterface as suggested by others. Or at the very least use Magento\Catalog\Model\ProductRepository::getById($id)
    – Frank
    Sep 14, 2017 at 16:09
  • Using ProductRepository does not create new objects, such as using a factory does Jul 6, 2018 at 12:34
6

The more Magento way of achieving this, although it's hard to tell since they most places throughout the core code do it differently is to use the product repository.

use Magento\Catalog\Model\ProductRepository;

protected $productRepository;

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

public function getMyProduct($productId)
{
    return $this->productRepository->getById($productId);
}
2
  • Correct, best and correct answer Aug 10, 2017 at 10:14
  • there is Marketplace issue with using productRepository in Magento 2
    – Waqar Ali
    Jul 2, 2019 at 10:29
0

for this you must use below

use \Magento\Catalog\Model\Resource\Product\CollectionFactory

$prod_id = "Your product id goes here";
$om         =   \Magento\Framework\App\ObjectManager::getInstance();
$pdata =   $om->create('Magento\Catalog\Model\Product')->load($prod_id);
-1

I have fetch product details using function in product view page like below.

Here Sm/Sawyer is my theme.

$_config = $this->helper('Sm\Sawyer\Helper\Data');
$productId = $this->getProduct()->getId();
$name = $this->getProduct()->getName();
$stock1 = $this->getProduct()->getQty();
$price = $this->getProduct()->getPrice();
2
  • very bad answer
    – LucScu
    Jul 11, 2017 at 7:05
  • Not helpful - just confusing Aug 10, 2017 at 10:15

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.