0% found this document useful (0 votes)
136 views

How To Get A Product by Its ID Programmatically - Development - Magento 2

See my Magento 2 extensions, installation, support, and payment gateway integration services. Subscribe to Twitter and Youtube. See also Magento 2 Design Themes.

Uploaded by

m1k13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

How To Get A Product by Its ID Programmatically - Development - Magento 2

See my Magento 2 extensions, installation, support, and payment gateway integration services. Subscribe to Twitter and Youtube. See also Magento 2 Design Themes.

Uploaded by

m1k13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

How to get a product by its ID programmatically?

Development
product-retrieving
product-repository
product-class
product
product-id

dmitry_fedyuk (Dmitry Fedyuk) 2016-02-26 15:25:44 UTC #1


Interface
Use the getById()method of the Magento\Catalog\Api\ProductRepositoryInterfaceinterface:

magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Api/ProductRepositoryInterface.php#L39-L49

39 /**
40 *Getinfoaboutproductbyproductid
41 *
42 *@paramint$productId
43 *@parambool$editMode
44 *@paramint|null$storeId
45 *@parambool$forceReload
46 *@return\Magento\Catalog\Api\Data\ProductInterface
47 *@throws\Magento\Framework\Exception\NoSuchEntityException
48 */
49 publicfunctiongetById($productId,$editMode=false,$storeId=null,$forceReload=false)

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

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

Usage
Example 1

magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Helper/Product.php#L416-L420

416 try{
417 $product=$this>productRepository>getById($productId,false,$this>_storeManager>getStore()>getId())
418 }catch(NoSuchEntityException$e){
419 returnfalse
420 }

Example 2

magento/magento2/blob/cf7df72/app/code/Magento/Bundle/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Bundle.php#L158-
L158

158 $linkProduct=$this>productRepository>getById($linkData['product_id'])

Example 3

magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Block/Product/View.php#L163-L175

163 /**
164 *Retrievecurrentproductmodel
165 *
166 *@return\Magento\Catalog\Model\Product
167 */
168 publicfunctiongetProduct()
169 {
170 if(!$this>_coreRegistry>registry('product')&&$this>getProductId()){
171 $product=$this>productRepository>getById($this>getProductId())
172 $this>_coreRegistry>register('product',$product)
173 }
174 return$this>_coreRegistry>registry('product')
175 }

Example 4

magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Console/Command/ImagesResizeCommand.php#L89-L89

89 $product=$this>productRepository>getById($productId)
Example 5

magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php#L180-L180

180 $product=$this>productRepository>getById($productId)

Example 6

magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php#L225-
L225

225 $linkProduct=$this>productRepository>getById($linkData['id'])

Example 7

magento/magento2/blob/cf7df72/app/code/Magento/Catalog/Controller/Product/Compare/Add.php#L28-L32

28 try{
29 $product=$this>productRepository>getById($productId,false,$storeId)
30 }catch(NoSuchEntityException$e){
31 $product=null
32 }

Example 8

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

23 try{
24 $product=$this>productRepository>getById($productId,false,$storeId)
25 }catch(NoSuchEntityException$e){
26 $product=null
27 }

Example 9

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

350 try{
351 $product=$this>productRepository>getById($productId)
352 }catch(NoSuchEntityException$e){
353 return''
354 }

Example 10

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

176 /**
177 *Retrieveproductviewpageurl
178 *
179 *@paramint|ModelProduct$product
180 *@returnstring|bool
181 */
182 publicfunctiongetProductUrl($product)
183 {
184 if($productinstanceofModelProduct){
185 return$product>getProductUrl()
186 }elseif(is_numeric($product)){
187 return$this>productRepository>getById($product)>getProductUrl()
188 }
189 returnfalse
190 }

Example 11

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

299 try{
300 $product=$this>productRepository>getById($product)
301 }catch(NoSuchEntityException$e){
302 returnfalse
303 }

Example 12

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

147 $product=$this>productRepository>getById($configureResult>getProductId(),false,$currentStoreId)

dmitry_fedyuk (Dmitry Fedyuk) 2016-02-26 15:33:39 UTC #2

See also:

How to load a product programmatically?


How to load a product by its ID programmatically?
How to load a product by its SKU programmatically?
How is a product loaded for a frontend product view?

Home Categories FAQ/Guidelines Terms of Service Privacy Policy

You might also like