Retrieving product information in Magento

Shop Supplies Product Info

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.

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.

You made it all the way down here so you must have enjoyed this post! You may also like:

Sorry, we can’t ship there Sasa Brankovic
Sasa Brankovic, | 12

Sorry, we can’t ship there

Shell script for converting configurable to grouped products Tsvetan Stoychev
Tsvetan Stoychev, | 5

Shell script for converting configurable to grouped products

Get rewritten product url in a different store Petar Sambolek
Petar Sambolek, | 9

Get rewritten product url in a different store

2 comments

  1. Hi

    I wonder if can help with a Magento question?

    Do you know how to add an image of the product to the order email template and a product link back to its product page on the Magento site? I can’t find any info on this, not sure if can be done, any help would be great.

    Thanks

    Dion

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.