现在的位置: 首页 > 综合 > 正文

Get Product ID and Product Name in Magento

2014年02月27日 ⁄ 综合 ⁄ 共 1564字 ⁄ 字号 评论关闭

In Magento eCommerce while working with catalog model, There arise the need to fetch product details from product id.

We can get all product details if we have product id.

But sometimes we only have product name, so we need to get product id for getting product details.

I am listing here both the method.

1) Product details from Product ID.

 
Code:
  1. <?php   
  2. $model = Mage::getModel('catalog/product'//getting product model   
  3.   
  4. $_product = $model->load($productid); //getting product object for particular product id   
  5.   
  6. echo $_product->getShortDescription(); //product's short description   
  7. echo $_product->getDescription(); // product's long description   
  8. echo $_product->getName(); //product name   
  9. echo $_product->getPrice(); //product's regular Price   
  10. echo $_product->getSpecialPrice(); //product's special Price   
  11. echo $_product->getProductUrl(); //product url   
  12. echo $_product->getImageUrl(); //product's image url   
  13. echo $_product->getSmallImageUrl(); //product's small image url   
  14. echo $_product->getThumbnailUrl(); //product's thumbnail image url     
  15.   
  16. ?>  

2) Product ID from Product Name

This is little bit complex. (If anybody has better way please post here)

Code:
  1. <?php   
  2. $product_name = 'Test Product'//product name   
  3. $model = Mage::getModel('catalog/product'//getting product model   
  4. $collection = $model->getCollection(); //products collection   
  5. foreach ($collection as $product//loop for getting products   
  6. {                      
  7.   
  8.     $model->load($product->getId());   
  9.     $pname = $model->getName();   
  10.     if(strcmp($pname,$product_name)==0)   
  11.     {   
  12.     $id = $product->getId();   
  13.     }   
  14. }   
  15. echo 'Required ID->'.$id//id of product   
  16. ?>  

 

抱歉!评论已关闭.