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 thoughts on “Get Product ID and Product Name in Magento

  1. Hello,

    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!

    1. <?php
      $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();
  2. 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

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

  3. 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(); }

  4. 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.

    1. Hi,

      Which Magento API will give the base cost and classification of a product ? ( is not providing the info about base cost and calssification of the product).?

      Thanks in advance..

  5. 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

  6. Hi

    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

      1. Hi,

        I have done as per the Magento instruction,. but I am getting the error as Invalid path ? can I have help from you to solve this path problem ?

  7. Product ID from Product Name :

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

    echo $_product->entity_id;

  8. Product Details by item id

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

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

    1. 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 ?


      1. i got it, i copy/pasted the code, and it replaced the “‘” with another type of quote :s

  10. 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 –



    1. @man_in_black – Are company address and company name custom attributes ??. You can check all the data by print_r($_product->getData()).

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top