Professional Documents
Culture Documents
How To Get A Product by Its ID Programmatically - Development - Magento 2
How To Get A Product by Its ID Programmatically - Development - Magento 2
How To Get A Product by Its ID Programmatically - Development - Magento 2
Development
product-retrieving
product-repository
product-class
product
product-id
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)
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)
See also: