Getting things in Magento by getModel and getData methods

Baghaus Product

If you are developing in Magento, sooner or alter you will need to obtain some information from database. This article will show you how to retrieve almost anything using getModel, getData and getter methods in general.

For an example, let say you are trying to retrieve products name, description and price. First thing you will need to do, is get products model:

$productModel = Mage::getModel('catalog/product');

By calling getModel(‘catalog/product’) you will get instance of Mage_Catalog_Model_Product class, defined in app/code/core/Mage/Catalog/Model/Product.php. But how does Magento know where to look for for class?

Well, first part of parameter ‘catalog/product’ comes from modules configuration, and is usually the same as name of module folder. If you take a look at app/code/core/Mage/Catalog/etc/config.xml, you will see:

<config><global>
        <models>
            <catalog>
                <class>Mage_Catalog_Model</class></catalog></models></global></config>

From here, you can see that all model whose name begins with Mage_Catalog_Model are defined within Model folder of this module. Second part of ‘catalog/product’ is telling Magento in which file is class defined. And in this case, it is Product.php.

Now that we have right model, next step would be to tell our model, which product should be loaded. This is done easily with load($id) method:

$product = $productModel->load(42);

Just to note here, 42 to is an example of id, and it should be replaced with id of product you are trying to load.

Now that we have loaded our product, there are two ways of retrieving data from object:

$name = $product->getName(); // same as $product->getData('name');
$description = $product->getData('description'); // same as $product->getDescription();

Both of them will work perfectly fine. But, when it comes to price, it’s a little bit different:

$price = $product->getPrice(); // same as $product->getData('price');
$finalPrice = $product->getFinalPrice();

In case that our product falls under any of price rules, those two variables would have different value. What is the difference? Variable $price would have products base price, visible from administration when you edit product. Variable $finalPrice, in this case, would have price with processed price rules.

Hope some of you find this useful. Feel free to provide some feedback.


About Stjepan Udovicic

Backend Developer

Stjepan likes to spend his free time programming and riding his motorbike.

Read more posts by Stjepan / Visit Stjepan's profile

12 comments

  1. I believe you guys have a typo in the 3rd paragraph.

    “By calling getModel(‘catalog/model’) you will get instance of Mage_Catalog_Model_Product class”

    I believe this should be:
    “By calling getModel(‘catalog/PRODUCT’) you will get an Instance of Mage_Catalog_Model_Product class”

  2. I am new to magento. I want to know the list of predefined models in magento like Mage::getModel(catelog/product). can you please post regardin this and “I want to get the data from table namely catalog_product_entity_varchar, do we have any model?? “sujjest me.

  3. Hi Branko,

    I have been following your posts for a long time. And each of them are very much helpful.

    Can you tell me how could I get the order Ids of the order those have been placed yesterday(What ever may be their status)???

    Currently I’m using the following code but it did not return the result as i expected. please help.

    <?php
    	require_once ("app/Mage.php");
    	ini_set("error_reporting",E_ALL);
    	ini_set("display_errors",true);
    	umask(0);
    	Mage::app('admin');
    
    	function getQtyOrdered($order)
    	{
    		$totalQty = 0;
    		foreach($order->getItemsCollection() as $item)
    		{
    			$qty = $item->getQtytoInvoice();
    			$totalQty = $totalQty + $qty;
    		}
    		return $totalQty;
    	}
    
    	$yesterday = date('Y-m-d', strtotime("yesterday"));
    	$orders = Mage::getModel('sales/order')->getCollection()
    		->addAttributeToFilter('created_at', array('from'  => $yesterday));
    
    	foreach($orders as $order)
    		{
    			echo $order->getIncrementId();
    			echo "<br/>";
    		}
    ?>
  4. Hi Branko,

    I have been following your posts for a long time. And each of them are very much helpful.

    Can you tell me how could I get the order Ids of the order those have been placed yesterday(What ever may be their status)???

    Currently I’m using the following code but it did not return the result as i expected. please help.

    getItemsCollection() as $item)
    {
    $qty = $item->getQtytoInvoice();
    $totalQty = $totalQty + $qty;
    }
    return $totalQty;
    }

    $yesterday = date(‘Y-m-d’, strtotime(“yesterday”));
    $orders = Mage::getModel(‘sales/order’)->getCollection()
    ->addAttributeToFilter(‘created_at’, array(‘from’ => $yesterday));

    foreach($orders as $order)
    {
    echo $order->getIncrementId();
    echo “”;
    }
    ?>

  5. Hello,

    I have to display product additional infor on product listing page…. i am trying to get it by below code
    getAdditionalData()):
    foreach ($_additional as $_data):
    if ($_data[‘label’] == ‘my-target-attribute’) echo $_data[‘value’];
    endforeach;
    endif;
    ?>
    but its returning me 0…..
    Please let me know how can i get products additional inforon product listing page..

    Thanks

  6. No, but you can do this:
    $field = ‘Firstname’;
    $method = ‘get’.$field;
    echo $billing->$method();

  7. Hi Branko,

    I have requirement something similar like you mentioned here

    can we do something like following
    $billing = $order->getBillingAddress();
    $field = “Firstname”;
    echo $billing->get . $field . “()” ;

    the reason to do this is to validate order data with order exported csv . can you please suggest me

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>.