Retrieving product information in Magento
If you are developing Magento template sooner or later you will need to obtain some product information and rearrange it to fit your template. One of the most useful function while working with Magento is Mage::getModel($modelClass).
Here is how you would use it to retrieve Magento product information Mage::getModel(’catalog/product’);.
This ‘catalog/product’ relates to app/code/core/Mage/Catalog/Model/Product.php file. If you open that file you will see it’s actually a class named Mage_Catalog_Model_Product. That same class is extending the Mage_Catalog_Model_Abstract class meaning it inherits all of it’s methods.
For the last few weeks I’ve been using NetBeans 6.5beta, regularly downloading night builds of it. Let’s suppose you open the /template/catalog/category/view.phtml file (ar any other from template folder) and you write down the “Mage_Catalog_Model_Product” + “Ctrl + Space” NetBeans will give you a code completion with all of the available methods that go along with the class.
All you need to do is something like
$cProduct = Mage::getModel(’catalog/product’);
Don’t let the name variable $cProduct confuse you. I’m using the letter “c” as a sort of personal standard, stands for custom. After you created the variable containing the model “catalog/product” you can then call all of the methods of Mage_Catalog_Model_Product on it.
Here is how:
$cProduct->load(152);
echo ‘<p>Product name: <strong>’ . $cProduct->getName() . ‘</strong></p>’;
echo ‘<p>Product price: <strong>’ . $cProduct->getPrice() . ‘</strong></p>’;
echo ‘<p>Product url: <strong>’ . $cProduct->getProductUrl() . ‘</strong></p>’;
echo ‘<p>Product Category: <strong>’ . $cProduct->getCategory() . ‘</strong></p>’;
echo ‘<p>Product image url: <strong>’ . $cProduct->getImageUrl() . ‘</strong></p>’;
echo ‘<p>Product status (this one is boolean): <strong>’ . $cProduct->getStatus() . ‘</strong></p>’;
echo ‘<p>Product weight: <strong>’ . $cProduct->getWeight() . ‘</strong></p>’;
Most important line in the above code is the $cProduct->load(152);. This is where you tell the $cProduct variable to point to product with id 152 (it was 152 on my working sample, it could be any other number). You can see the products id value if you log on to the Admin interrface and go to Catalog > Manage products > And look into the table listing all the products.
Here are some screenshots I made while writing this for you to see.

Getting models by getModel() function isn’t limited to product only. I suggest you play around with as much models as you can. Results can be very useful.
Hope some of you find this useful. Feel free to provide some feedback. I’ll try to answer any question on my free time.







Dear branko;
Though the hack u did regarding getModel() is awesome in terms that it helps to reterive useful information modules wise. I’ll try that tomorrow. But can you tell me is there any way we can do the same using Magento CMS pages right away?
Responce will be great…
hey guys, not sure what you’re trying to do but I’ve been struggling with how to access the data, such as how to get the page_id of a cms page being viewed to then render code accordingly. To get the page_id, try this:
$idParams = $this->getRequest()->getParams();
print_r($idParams);
Hi
how can we fetch the additional product attributes. The Mage::getModel(’catalog/product’); only fetches the default set of products attributes. Can you tell how to fetch my own entered attributes for a product or can you tell where in the database the magento stores extra attributes values for a product. I know you can help
Keep the good work doing.
Hello guys, i was wondering how did you get that look of the magento phtml files with the red border on the product page.
You login to Magetno Admin then go to System > Configuration, Select the Current configuration scope on the top left side (its usually the Main Website) then go down to the bottom left tab Developer > Debug > Then set the Template Path Hints to Yes.
thanks a lot branko !
Is there a way to pull the gallery images using this method?
great post Branko!
you are greatly helping to understand all basic logics in Magento.
thanks again and keep blogging
Hi,
Nice blog , really I liked your article on “Getting things in Magento by getModel and getData methods”.
Can you please explain the use of collection and how the flow works in that case.
It will be very kind , if you explain that.
Regards
Surjan