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 ?>
Used:-
$_specialPriceInclTax = $_taxHelper->getPrice($_product, $_product->getSpecialPrice(), true);
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
Tks a lot 😀
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
Thank DWRoot that worked out great 🙂
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
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
@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.
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/
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 ?
Product Id from Product Name
$collection = Mage::getModel(‘catalog/product’)
->getCollection()
->addAttributeToFilter(‘name’,$pname)
;
$product = $collection->getFirstItem();
echo $product->getName();
It works. Thanks
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.
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
Let me know whether this is what you require
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
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
You can make an array of the products data and can display the data as per your convenience.
nice article ! save !
This is also great post, Keep it up buddy. 🙂
You write:
$collection = $tmp->getCollection(); //products collection
Where comes $tmp from????
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 😉
Interesting post – you’ve done a thorough job 🙂