How to get a product by its ID programmatically?

Interface

Use the getById() method of the [Magento\Catalog\Api\ProductRepositoryInterface] (https://github.com/magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Api/ProductRepositoryInterface.php) interface:

It throws a \Magento\Framework\Exception\NoSuchEntityException exception if a product with the given ID is not found.

Implementation

How does \Magento\Catalog\Api\ProductRepository::getById() work?

Usage

Example 1

Example 2

Example 3

Example 4

Example 5

Example 6

Example 7

Example 8

https://github.com/magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Controller/Product/Compare/Remove.php#L23-L27

Example 9

https://github.com/magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Helper/Data.php#L350-L354

Example 10

https://github.com/magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Helper/Product.php#L176-L190

Example 11

https://github.com/magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Helper/Product.php#L299-L303

Example 12

https://github.com/magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Helper/Product/Composite.php#L147-L147

1 Like

See also: