Get Product ID and Product Name in Magento

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.

$model = Mage::getModel('catalog/product') //getting product model

$_product = $model->load($productid); //getting product object for particular product id

echo $_product->getShortDescription(); //product's short description
echo $_product->getDescription(); // product's long description
echo $_product->getName(); //product name
echo $_product->getPrice(); //product's regular Price
echo $_product->getSpecialPrice(); //product's special Price
echo $_product->getProductUrl(); //product url
echo $_product->getImageUrl(); //product's image url
echo $_product->getSmallImageUrl(); //product's small image url
echo $_product->getThumbnailUrl(); //product's thumbnail image url	


2) Product ID from Product Name

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

$product_name = 'Test Product'; //product name
$model = Mage::getModel('catalog/product') //getting product model
$collection = $model->getCollection(); //products collection
foreach ($collection as $product) //loop for getting products
    $pname = $model->getName();	
	$id = $product->getId();
echo 'Required ID->'.$id; //id of product

112 Responses to “Get Product ID and Product Name in Magento”

  1. Scott says:

    Huge help, thanks a lot!

  2. Lapinou says:


    Thank you for you code, it’s really helpfull!

    But I have a question: How can I get all id product and store them in an array ?

    Because I want to update my Magento database to set the stock quantity receives from a web service.

    Thank you so much for your help!

    • Pragnesh says:
      $model = Mage::getModel('catalog/product') //getting product model
      $collection = $model->getCollection(); //products collection
      $ids = array();
      foreach ($collection as $product) //loop for getting products
          $ids[] = $product->getId();
  3. Hello, can you services me by giving answers to this question – how do i take my ecommerce internationally?

  4. Upma says:

    How to set Add to cart button.
    I have tried:
    isSaleable()): ?>
    <button type="button" title="__(‘Add to Cart’) ?>” class=”button btn-cart” onclick=”setLocation(‘getAddToCartUrl($_product) ?>’)”>__(‘Add to Cart’) ?>

    __(‘Out of stock’) ?>

    But not working

    • Pragnesh says:

      The code snippet is not correct, please refer the add to cart button found in default magento design, path goes like this

  5. gourav godha says:

    thnx!!! such a grt example

  6. Daniel says:

    Great example.thanks!

  7. The Creation says:

    i want to add only product, so if i add the following code in any page, will that work?

    getName(); //product name


  8. nks says:

    I am using soap api to access magento data for a third party application but when i try to add coupon code in shoping cart then it is not working and throes an error: Coupon code is not valid.
    can anybody help me how to solve this issue.
    my code is..

    try {
    $resultCartCouponAdd = $soap->call(

    catch (Exception $e)

    {echo ‘…ERROR: ‘, $e->getMessage(); }

  9. This is the way I did always. But I believe this is not the best method.

    What if there is a large amounts of products in the database? Select all of them is a big resources.

  10. Kent Yip says:

    product name you want to find and get their product id example;

    $pname = array('My Product Name', 'His Product Name');

    $pcollection = Mage::getSingleton('catalog/product')
    ->addFieldToFilter('name', array('in' => $pname));

    now you have a collection of products whos’ name are ‘My Product Name’ and ‘His Product Name’

    many more options for the addFieldToFilter at

    scroll to middle of page and see all the options

  11. shameem356 says:


    I am trying make a new API in magento 1.7 version.I craeted a three XML file and one php file.
    XML file are the following

  12. shameem356 says:


    How to check a particular product is belongs to which store ?

  13. Jamal says:

    Product ID from Product Name :

    $_product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘name’, ‘Anashria Womens Premier Leather Sandal’);

    echo $_product->entity_id;

  14. Abdulkadir says:

    this is a nice tutorial

  15. Manoj Bhardwaj says:

    Product Details by item id

    $custom = Mage::getModel(‘catalog/product’)->load($_item->getProductId());
    echo $custom->getShortDescription()

  16. Manoj Bhardwaj says:

    $custom = Mage::getModel(‘catalog/product’)->load($_item->getProductId());
    echo $custom->getShortDescription()

    • Gert says:

      I get php error :
      Call to a member function load() on a non-object in /var/www/myshop/app/design/frontend/base/default/template/checkout/cart/item/default.phtml on line 43

      what could be the problem here ?


  17. magento says:

    Very helpful info here. Product ID is easy to use with this post!

  18. man_in_black says:

    well thanks i have got it but can you also tell me how i will get the company address and company name and also product starting date and ending date i mean the day product will expire ?
    is it like this –



Leave a Reply