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. Used:-

    $_specialPriceInclTax = $_taxHelper->getPrice($_product, $_product->getSpecialPrice(), true);

  2. Hi DWRoot

    $_finalPriceInclTax = $_taxHelper->getPrice($_product, $_product->getFinalPrice(), true);

    works great for normal price but is there a way to get Special Price with VAT?

    Thanks

  3. Hello 2all! Please, help me, I try to load in main menu 1-level categories with products, but I have some issues with products names, it returns only url of the product. Below is my code. Thnx in advance.

    getIsActive()):
    $cur_category=Mage::getModel(‘catalog/category’)->load($_main_category->getId());
    $layer = Mage::getSingleton(‘catalog/layer’);
    $layer->setCurrentCategory($cur_category);

    /* Write the main categories */
    ?>
    <a href="getCurrentCategory()->getUrl()?>”>getCurrentCategory()->getName();?>

    load(CATEGORY_ID);
    $_productCollection = $newCarCollection->getProductCollection();
    $_helper = $this->helper(‘catalog/output’);
    ?>
    count()): ?>
    __(‘There are no products matching the selection.’) ?>

    count() ?>
    getColumnCount(); ?>

    <a href="getProductUrl() ?>” title=””>getName(); ?>

    setCurrentCategory($_current_category); ?>

    1. @Igor – You need to execute a loop of product collection, directly you cannot display product information, product collection is an array of objects

  4. How would I add to the above, to return all the products with price including tax?
    Note: Our products is entered in Magento excluding Tax.

    Using?:-
    echo $product->getPrice().”;

    Only show excluding Tax

    1. @iRss – Try this

      <?php
      $_taxHelper  = Mage::helper('tax');
      $_product = Mage::getModel('catalog/product')->load(1); //where 1 is the product id for which the price needed
      $_finalPriceInclTax = $_taxHelper->getPrice($_product, $_product->getFinalPrice(), true); 
      ?>
      
  5. how can i get the id of the search product. I want to know it as i have tried so many things could not able to get the product id. Can any one help me out. I am using the following code, bt it is displaying all the categories. how can i filter it according to search product.

  6. @jerseys it’s damn hard at first but once you get a hold of the data structures i.e. getmodel(), load(), getX() etc it gets a lot easier. It’s just a shame Magento’s not a little more logical sometimes.

  7. Hello All,
    I want to store categories like store name is
    Store1 Store2 Store3 Store4

    If mouse over on store1 or store2… store4 it will view drop down that store parent categories. in store want to store category in drop down after mouse over on store name….

  8. Product Id from Product Name
    $collection = Mage::getModel(‘catalog/product’)
    ->getCollection()
    ->addAttributeToFilter(‘name’,$pname)
    ;
    $product = $collection->getFirstItem();
    echo $product->getName();

  9. This code

    $model = Mage::getModel(‘catalog/product’);
    $_product = $model->load($productid);

    is not working in my tabs.phtml file. located here:
    app\design\frontend\default\default\template\easytabs\tabs.phtml

    This line

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

    loads nothing at all.

    Only if I put the value there manually, like:

    $_product = $model->load(34);

    Then it loads the product with id egauls 34.

    So, could you give me some advice how to get the product id working? Thanks in advance.

    1. There should exist a product id for getting the attributes value of a particular Product (i.e $productid).

      As its not defined in your code, so nothing is working.

      If you don’t have product id , you can get like this

      <?php
      $model = Mage::getModel('catalog/product'); //getting product model
      $collection = $model->getCollection(); //products collection
      foreach ($collection as $product) //loop for getting products
      {
      	echo $product->getId().'<br/>';
      
      }
      ?>
      

      Let me know whether this is what you require

    1. We can use the product collection as shown in the post, you will get catalog products data

      <?php
      $model = Mage::getModel('catalog/product'); //getting product model
      $collection = $model->getCollection(); //products collection
      foreach ($collection as $product) //loop for getting products
      {					
      	echo $product->getId().'<br/>';
          
      }
      ?>
      
  10. You write:

    $collection = $tmp->getCollection(); //products collection

    Where comes $tmp from????

  11. Its easier to go the source:

    To find a product named “b1” use:

    SELECT
    entity_id
    FROM
    catalog_product_entity_varchar
    JOIN eav_attribute ON catalog_product_entity_varchar.attribute_id = eav_attribute.attribute_id
    WHERE
    catalog_product_entity_varchar.entity_type_id = 4 AND
    eav_attribute.attribute_code = “name” AND
    catalog_product_entity_varchar.value = “b1”;

    I hope that is readable (there is no comment preview 😉

Leave a Reply

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

Scroll to top