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.

<?php
$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)

<?php
$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
{					
					
    $model->load($product->getId());
    $pname = $model->getName();	
    if(strcmp($pname,$product_name)==0)
    {
	$id = $product->getId();
    }				
}
echo 'Required ID->'.$id; //id of product
?>

112 thoughts on “Get Product ID and Product Name in Magento

  1. DWRoot: thanks for your support (i’m a coworker of Peter). We solved the problem: there was a setting missing:

    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    With the ADMIN_STORE_ID being set, the ->save() function does not throw an error anymore.

  2. @DWRoot – No problem, the affected website is still under construction.

    The Exception Message is:
    PHP Catchable Fatal Error: Argument 3 passed to Mage_Catalog_Model_Resource_Abstract::_canUpdateAttribute() must be an array, null given, called in /home/passionfrance.de/public_html/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1225 and defined in /home/passionfrance.de/public_html/magento/app/code/core/Mage/Catalog/Model/Resource/Abstract.php line 543

    Any ideas what to do?

    1. @Peter – What I am concluding is that, one or more product attributes which you are trying to edit, is passed null value, please try identifying what values are passed before editing product.

  3. @DWRoot – Thanks für your quick reply. I load it already:

    $model = Mage::getModel(‘catalog/product’);
    $collection = $model->getCollection(); //products collection
    foreach ($collection as $product) { //loop for getting products
    $model->load($product->getId());
    $this->cli_echo(“found product ‘”.$model->getName().”‘\n”);
    try {
    $model->setName(‘TestTestTest’); //will be something useful later
    $model->save();
    } catch (Exception $e) {
    $this->cli_echo(“Exception during saving: “.$e->getMessage().”\n”);
    }
    }

    I get the Exception from above.

    Any ideas what’s wrong with it?

    1. @Peter – Sorry for late reply, I tried your code and I did not found any issue, can you kindly tell me what exception message are you getting

  4. I would like to change some data and save the article.

    I used method 2 from the post above and added:

    $model->setName(‘TestTestTest’);
    $model->save();

    I expected the product name to be changed and saved into the db. But instead, I get the error:

    PHP Catchable Fatal Error: Argument 3 passed to Mage_Catalog_Model_Resource_Abstract::_canUpdateAttribute() must be an array, null given, called in /home/passionfrance.de/public_html/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1225 and defi ned in /home/passionfrance.de/public_html/magento/app/code/core/Mage/Catalog/Model/Resource/Abstract.php line 543

    Has anyone hints for me?

    Best regards,
    Peter

    1. @Peter – If you are creating a new product, you need to setId to null, or if you are editing the existing product, you need to load the product first, hope this helps you.

  5. Hey, what u use $_product (variable name) instead of $product … (without “_” … underline) ??

  6. i want to fetch product details from magento using API in our project. user will enter API, username, all product of magento should be fetched.
    any one can help me for this.

  7. @khamar – Your code is incorrect, without PHP quotes you are using the PHP variables, check any core Magento view( phtml) files for reference

  8. hi

    <a href="#" onclick="setLocation('$row[‘product_id’],’qty’=>1) ?>’)”><img src="getSkinUrl(‘images/add-to-cart-btn.jpg ‘) ?>” alt=”Add to Cart”/>

    i using this code redirect the add to cart page but product_id through not display ..
    so
    pls help>>???

    1. @khamar – Use this code

      <?php $pid = $row['product_id']; ?>
      <a href="#" onclick="setLocation(<?php echo $pid ?>,1)"><img src="<?php echo $this->getSkinUrl('images/add-to-cart-btn.jpg') ?>" alt="Add to Cart"/>
      
  9. 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

    ?>

  10. $_product = $model->load($productid);

    Error For this line Undefine Variable..

    I m Using This Code To Display Product Information But Not Work Pls Reply..

    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

    ?>

  11. foreach(Mage::getModel(‘catalog/product’)->getCollection()->getData() as $product){

    echo Mage::getModel(‘catalog/product’)->load($product[‘entity_id’])->getName();

    }

  12. I’m sorry but this code i what i mean:

    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

    ?>

    Sorry, i wasnt able to copy it right. But this code is what i mean.

  13. What are the include file i need to declare to make this code work:

    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

    ?>
    I’m barely new into this and i dont know where to start, i need to add content into the database.

    Can you help me with this, please?

Leave a Reply

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

Scroll to top