20

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
    Commented Nov 30, 2015 at 6:35

6 Answers 6

23

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. Commented Feb 11, 2018 at 16:38
  • its not recommended to use ProductRepository in Magneto 2
    – Waqar Ali
    Commented Jul 2, 2019 at 10:30
  • @WaqarAli, then what is recommended?
    – Black
    Commented Jan 9, 2020 at 16:13
15

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 Commented Sep 10, 2017 at 16:44
  • 3
    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
    Commented Sep 14, 2017 at 16:09
  • Using ProductRepository does not create new objects, such as using a factory does Commented 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 Commented Aug 10, 2017 at 10:14
  • there is Marketplace issue with using productRepository in Magento 2
    – Waqar Ali
    Commented 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);
-2

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();
3
  • very bad answer
    – LucScu
    Commented Jul 11, 2017 at 7:05
  • Not helpful - just confusing Commented Aug 10, 2017 at 10:15
  • Explain your answer, and where is this code defined? $this->getProduct() as this is responsible to bring the product object.
    – Duke
    Commented Apr 25 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.