Product Details in Magento
Load Product using Id in Magento
$product = Mage::getModel('catalog/product')->load($id); //returns product data as a varien object $product = Mage::getModel('product/product')->load($id)->getData(); //returns product data as an array
Load Product using Sku in Magento
$product = Mage::getModel("catalog/product")->loadByAttribute("sku", $sku); $product = Mage::getModel('catalog/product')->load($product->getIdBySku($sku)); //this one is more efficient
Getting Product Details
After loading product as an object, you can assess variour methods.
$product->getShortDescription(); //returns product's short description $product->getDescription(); //returns product's long description $product->getName(); //returns product name $product->getPrice(); //returns product's regular Price $product->getSpecialPrice(); //returns product's special Price $product->getProductUrl(); //returns product url $product->getImageUrl(); //returns product's image url $product->getSmallImageUrl(); //returns product's small image url $product->getThumbnailUrl(); //product's thumbnail image url
If you have data in array format, use var_dump to check what your variable holds.
Formatting Product Price in Magento
getPrice() returns product price without currency sign. To properly format product price use the code below:
$product = Mage::getModel("catalog/product")->load($id); $price = $product->getPrice(); $formattedPrice = Mage::helper('core')->currency($price, true, false); //another way $formattedPrice = Mage::helper('core')->formatPrice($price, true); //getting price in another currency Mage::app()->getLocale()->currency($currencyCode)->toCurrency($price);
Get Product Category in Magento
$product = Mage::getModel('catalog/product')->load($id); $categoryIds = $product->getCategoryIds(); //returns category ids since a product can be associated with more than one category foreach($categoryIds as $category_id) { $category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($category_id); echo $category->getName(); //returns category name }