18

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 ?

  • @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 '15 at 6:35
21

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

  • 1
    You should use \Magento\Catalog\Api\ProductRepositoryInterface::getById instead. – Thiago Lima Feb 11 '18 at 16:38
  • its not recommended to use ProductRepository in Magneto 2 – Waqar Ali Jul 2 at 10:30
11

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>);
    
  • there is no class \Magento\Catalog\Model\ProductFactory in Magento 2.1.7 – shahir hajir Sep 10 '17 at 16:44
  • 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 '17 at 16:09
  • Using ProductRepository does not create new objects, such as using a factory does – Raul Sanchez Jul 6 '18 at 12:34
9

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
5

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);
}
  • Correct, best and correct answer – Matthias Kleine Aug 10 '17 at 10:14
  • there is Marketplace issue with using productRepository in Magento 2 – Waqar Ali Jul 2 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();
  • very bad answer – LucScu Jul 11 '17 at 7:05
  • Not helpful - just confusing – Matthias Kleine Aug 10 '17 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.