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
- February 26, 2014 @ 17:15:08 [Current Revision] by admin
- February 26, 2014 @ 17:16:10 [Autosave] by admin
- February 26, 2014 @ 17:15:08 by admin
- February 26, 2014 @ 17:14:51 by admin
Revision Differences
February 26, 2014 @ 17:14:51 | Current 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: <?php | Unchanged: <?php | ||
Unchanged: $model = Mage::getModel( 'catalog/ product') //getting product model | Unchanged: $model = Mage::getModel( 'catalog/ product') //getting product model | ||
Unchanged: $_product = $model->load( $productid); //getting product object for particular product id | Unchanged: $_product = $model->load( $productid); //getting product object for particular product id | ||
Unchanged: echo $_product-> getShortDescription(); //product's short description | Unchanged: echo $_product-> getShortDescription(); //product's short description | ||
Unchanged: echo $_product-> getDescription(); // product's long description | Unchanged: echo $_product-> getDescription(); // product's long description | ||
Unchanged: echo $_product->getName(); //product name | Unchanged: echo $_product->getName(); //product name | ||
Unchanged: echo $_product->getPrice(); //product's regular Price | Unchanged: echo $_product->getPrice(); //product's regular Price | ||
Unchanged: echo $_product-> getSpecialPrice(); //product's special Price | Unchanged: echo $_product-> getSpecialPrice(); //product's special Price | ||
Unchanged: echo $_product-> getProductUrl(); //product url | Unchanged: echo $_product-> getProductUrl(); //product url | ||
Unchanged: echo $_product-> getImageUrl(); //product's image url | Unchanged: echo $_product-> getImageUrl(); //product's image url | ||
Unchanged: echo $_product-> getSmallImageUrl(); //product's small image url | Unchanged: echo $_product-> getSmallImageUrl(); //product's small image url | ||
Unchanged: echo $_product-> getThumbnailUrl(); //product's thumbnail image url | Unchanged: echo $_product-> getThumbnailUrl(); //product's thumbnail image url | ||
Unchanged: ?> | Unchanged: ?> | ||
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 Name | Unchanged: 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: <?php | Unchanged: <?php | ||
Unchanged: $product_name = 'Test Product'; //product name | Unchanged: $product_name = 'Test Product'; //product name | ||
Unchanged: $model = Mage::getModel( 'catalog/ product') //getting product model | Unchanged: $model = Mage::getModel( 'catalog/ product') //getting product model | ||
Unchanged: $collection = $model->getCollection(); //products collection | Unchanged: $collection = $model->getCollection(); //products collection | ||
Unchanged: foreach ($collection as $product) //loop for getting products | Unchanged: foreach ($collection as $product) //loop for getting products | ||
Unchanged: { | Unchanged: { | ||
Unchanged: | Unchanged: | ||
Unchanged: $model->load( $product->getId()); | Unchanged: $model->load( $product->getId()); | ||
Unchanged: $pname = $model->getName(); | Unchanged: $pname = $model->getName(); | ||
Unchanged: if(strcmp($pname,$product_name)==0) | Unchanged: if(strcmp($pname,$product_name)==0) | ||
Unchanged: { | Unchanged: { | ||
Unchanged: $id = $product->getId(); | Unchanged: $id = $product->getId(); | ||
Unchanged: } | Unchanged: } | ||
Unchanged: } | Unchanged: } | ||
Unchanged: echo 'Required ID->'.$id; //id of product | Unchanged: echo 'Required ID->'.$id; //id of product | ||
Deleted: ?> | |||
Deleted: </pre> | Added: ?></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& link=http%3A%2F%2Fblog.decryptweb.com%2Fproduct- id-and-product- name%2F& notes=&short_link=& shortener=google&shortener_ key=&v=1&apitype= 1&apikey= 8afa39428933be41f8afdb8ea21a495c& source=Shareaholic- Publishers& template=& service=219&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.