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 ?>
wow very nice! I was looking to show a particular product description outside magento directory 🙂
Thank you!
how do i get the product id in onepage.php ? can you tell me just need to get the product id of the product the user currently checking out
@man_in_black – Please check this post of mine Get cart products in Magento. I hope its useful
really help full
thanks for this all stuff
DWRoot: thanks for your support (i’m a coworker of Peter). We solved the problem: there was a setting missing:
With the ADMIN_STORE_ID being set, the ->save() function does not throw an error anymore.
@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?
@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.
@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?
@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
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.
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
thanks fro posing this post great work
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
@khamar – Your code is incorrect, without PHP quotes you are using the PHP variables, check any core Magento view( phtml) files for reference
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 – Use this code
Thanx Bro Its Working.
.
@Khamar – You are welcome
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
?>
$_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
?>
@Khamar – $productid is the id of the product for which you need the required data
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.
foreach(Mage::getModel(‘catalog/product’)->getCollection()->getData() as $product){
echo Mage::getModel(‘catalog/product’)->load($product[‘entity_id’])->getName();
}
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.
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?
Awesome, thank you! Managed to get this working. Now for the tricky part… canonicals 😛