You are viewing an old revision of this post, from February 26, 2014 @ 17:14:51. See below for differences between this version and the current revision.

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

Revisions

Revision Differences

February 26, 2014 @ 17:14:51Current Revision
Content
Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">
Unchanged: In Magento eCommerce while working with catalog model, There arise the need to fetch product details from product id.Unchanged: In Magento eCommerce while working with catalog model, There arise the need to fetch product details from product id.
Unchanged: </p>Unchanged: </p>
Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">
Unchanged: We can get all product details if we have product id.Unchanged: We can get all product details if we have product id.
Unchanged: </p>Unchanged: </p>
Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">
Unchanged: But sometimes we only have product name, so we need to get product id for getting product details.Unchanged: But sometimes we only have product name, so we need to get product id for getting product details.
Unchanged: </p>Unchanged: </p>
Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">
Unchanged: I am listing here both the method.Unchanged: I am listing here both the method.
Unchanged: </p>Unchanged: </p>
Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">
Unchanged: 1) Product details from Product ID.Unchanged: 1) Product details from Product ID.
Unchanged: </p>Unchanged: </p>
Unchanged: <pre class="brush: php; title: ; notranslate" style="color: rgb(65, 63, 54); line-height: 22px; background-color: rgb(244, 239, 214);" title="">Unchanged: <pre class="brush: php; title: ; notranslate" style="color: rgb(65, 63, 54); line-height: 22px; background-color: rgb(244, 239, 214);" title="">
Unchanged: &lt;?phpUnchanged: &lt;?php
Unchanged: $model = Mage::getModel( &#39;catalog/ product&#39;) //getting product modelUnchanged: $model = Mage::getModel( &#39;catalog/ product&#39;) //getting product model
Unchanged: $_product = $model-&gt;load( $productid); //getting product object for particular product idUnchanged: $_product = $model-&gt;load( $productid); //getting product object for particular product id
Unchanged: echo $_product-&gt; getShortDescription(); //product&#39;s short descriptionUnchanged: echo $_product-&gt; getShortDescription(); //product&#39;s short description
Unchanged: echo $_product-&gt; getDescription(); // product&#39;s long descriptionUnchanged: echo $_product-&gt; getDescription(); // product&#39;s long description
Unchanged: echo $_product-&gt;getName(); //product nameUnchanged: echo $_product-&gt;getName(); //product name
Unchanged: echo $_product-&gt;getPrice(); //product&#39;s regular PriceUnchanged: echo $_product-&gt;getPrice(); //product&#39;s regular Price
Unchanged: echo $_product-&gt; getSpecialPrice(); //product&#39;s special PriceUnchanged: echo $_product-&gt; getSpecialPrice(); //product&#39;s special Price
Unchanged: echo $_product-&gt; getProductUrl(); //product urlUnchanged: echo $_product-&gt; getProductUrl(); //product url
Unchanged: echo $_product-&gt; getImageUrl(); //product&#39;s image urlUnchanged: echo $_product-&gt; getImageUrl(); //product&#39;s image url
Unchanged: echo $_product-&gt; getSmallImageUrl(); //product&#39;s small image urlUnchanged: echo $_product-&gt; getSmallImageUrl(); //product&#39;s small image url
Unchanged: echo $_product-&gt; getThumbnailUrl(); //product&#39;s thumbnail image url Unchanged: echo $_product-&gt; getThumbnailUrl(); //product&#39;s thumbnail image url
Unchanged: ?&gt;Unchanged: ?&gt;
Unchanged: </pre>Unchanged: </pre>
Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">
Unchanged: 2) Product ID from Product NameUnchanged: 2) Product ID from Product Name
Unchanged: </p>Unchanged: </p>
Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">Unchanged: <p style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214);">
Unchanged: This is little bit complex. (If anybody has better way please post here)Unchanged: This is little bit complex. (If anybody has better way please post here)
Unchanged: </p>Unchanged: </p>
Unchanged: <pre class="brush: php; title: ; notranslate" style="color: rgb(65, 63, 54); line-height: 22px; background-color: rgb(244, 239, 214);" title="">Unchanged: <pre class="brush: php; title: ; notranslate" style="color: rgb(65, 63, 54); line-height: 22px; background-color: rgb(244, 239, 214);" title="">
Unchanged: &lt;?phpUnchanged: &lt;?php
Unchanged: $product_name = &#39;Test Product&#39;; //product nameUnchanged: $product_name = &#39;Test Product&#39;; //product name
Unchanged: $model = Mage::getModel( &#39;catalog/ product&#39;) //getting product modelUnchanged: $model = Mage::getModel( &#39;catalog/ product&#39;) //getting product model
Unchanged: $collection = $model-&gt;getCollection(); //products collectionUnchanged: $collection = $model-&gt;getCollection(); //products collection
Unchanged: foreach ($collection as $product) //loop for getting productsUnchanged: foreach ($collection as $product) //loop for getting products
Unchanged: { Unchanged: {
Unchanged: Unchanged:
Unchanged: $model-&gt;load( $product-&gt;getId());Unchanged: $model-&gt;load( $product-&gt;getId());
Unchanged: $pname = $model-&gt;getName(); Unchanged: $pname = $model-&gt;getName();
Unchanged: if(strcmp($pname,$product_name)==0)Unchanged: if(strcmp($pname,$product_name)==0)
Unchanged: {Unchanged: {
Unchanged: $id = $product-&gt;getId();Unchanged: $id = $product-&gt;getId();
Unchanged: } Unchanged: }
Unchanged: }Unchanged: }
Unchanged: echo &#39;Required ID-&gt;&#39;.$id; //id of productUnchanged: echo &#39;Required ID-&gt;&#39;.$id; //id of product
Deleted: ?&gt; 
Deleted: </pre> Added: ?&gt;</pre>
Deleted: <div class="shr-publisher-122 shareaholic-show-on-load" style="color: rgb(65, 63, 54); font-family: Arial; line-height: 22px; background-color: rgb(244, 239, 214); overflow: hidden; height: 78px;"> 
Deleted: <div class="shr-bookmarks shr-bookmarks-bg-shr" style="margin: 20px 0px 8px 10px; height: 78px; overflow: hidden; padding: 26px 0px 0px 10px; background-image: url(http://blog.decryptweb.com/ wp-content/plugins/ sexybookmarks/ images/share-enjoy.png); background-color: transparent; clear: both !important; background-repeat: no-repeat no-repeat;"> 
Deleted: <ul class="shr-socials" style="list-style-image: url(http://blog.decryptweb.com/ wp-content/themes/ Bold/images/ bullet.gif); line-height: 23px; width: 572px; margin: 0px !important; padding-right: 0px !important; padding-left: 0px !important; float: left !important; background-image: none !important; background-color: transparent !important; border: 0px none !important; outline: none 0px !important;"> 
Deleted: <li class="shr-219 shareaholic" style="background-image: url(http://blog.decryptweb.com/ wp-content/plugins/ sexybookmarks/ spritegen_default/sprite.png) !important; display: inline !important; float: left !important; list-style-type: none !important; padding: 0px !important; height: 29px !important; width: 60px !important; cursor: pointer !important; margin: 3px 0px 0px !important; background-color: transparent !important; border: 0px none !important; outline: none 0px !important; clear: none !important; background-position-x: -13140px !important; background-position-y: 100%;"> 
Deleted: <a class="external" href="http:// www.shareaholic.com/api/share/ ?title=Get+Product+ID+and+ Product+Name+ in+Magento&amp; link=http%3A%2F%2Fblog.decryptweb.com%2Fproduct- id-and-product- name%2F&amp; notes=&amp;short_link=&amp; shortener=google&amp;shortener_ key=&amp;v=1&amp;apitype= 1&amp;apikey= 8afa39428933be41f8afdb8ea21a495c&amp; source=Shareaholic- Publishers&amp; template=&amp; service=219&amp;ctype=" orig_title="Post to Blogger" rel="nofollow" style="outline: none; text-decoration: none !important; color: rgb(65, 63, 54); display: block !important; width: 60px !important; height: 29px !important; text-indent: -9999px !important; background-color: transparent !important; border: 0px none !important;" target="_blank">Post to Blogger</a> 
Deleted: </li> 
Deleted: </ul> 
Deleted: </div> 
Deleted: </div> 

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply