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

Interesting post – you’ve done a thorough job
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
You write:
$collection = $tmp->getCollection(); //products collection
Where comes $tmp from????
This is also great post, Keep it up buddy.
nice article ! save !
Thanks for your post, it’s great.
I have one more question, how can i echo names&links of previous product and next products on current products page? thanks
But what if we want detail of all products ans we dont have name and id of product
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/>'; } ?>You can make an array of the products data and can display the data as per your convenience.
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.
It works. Thanks
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
Product Id from Product Name
$collection = Mage::getModel(‘catalog/product’)
->getCollection()
->addAttributeToFilter(‘name’,$pname)
;
$product = $collection->getFirstItem();
echo $product->getName();
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….
@salim – Can you explain bit more, I am not getting exactly what’s the requirement ?
i want to lean magento but now it’s hard to me.
@jerseys – If you know Zend Framework, it will helpful in learning Magento, also try Magento Wiki – http://www.magentocommerce.com/wiki/
@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.
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.
@saurabh – Can you explain in details, whats the requirement, as it s seems to be unclear now
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
@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); ?>Thank DWRoot that worked out great
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); ?>
@Igor – You need to execute a loop of product collection, directly you cannot display product information, product collection is an array of objects
Tks a lot
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
Used:-
$_specialPriceInclTax = $_taxHelper->getPrice($_product, $_product->getSpecialPrice(), true);
Awesome, thank you! Managed to get this working. Now for the tricky part… canonicals
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?
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.
@azir – Can you please tell me whats your requirement
@azir – No need of including any file, you can write this code in any phtml or php file, only ‘$productid’ is required.
foreach(Mage::getModel(‘catalog/product’)->getCollection()->getData() as $product){
echo Mage::getModel(‘catalog/product’)->load($product['entity_id'])->getName();
}
Hi there, You’ve done a fantastic job. I’ll certainly digg it and personally suggest to my friends. I am sure they will be benefited from this website.
$_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
?>
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
?>
@Khamar – $productid is the id of the product for which you need the required data
Thanx Bro Its Working.
.
@Khamar – You are welcome
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>>???
@khamar – Your code is incorrect, without PHP quotes you are using the PHP variables, check any core Magento view( phtml) files for reference
@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"/>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.
@saurabh – You can refer this post on my blog for API and Product Listing – Magento SOAP API
thanks fro posing this post great work
Hey, what u use $_product (variable name) instead of $product … (without “_” … underline) ??
@Matheus – You can use ‘underscore’ in a variable name, its a common practice used in Magento
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
@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.