Magento 1x models


Product model

This function will fetch all the product information in the database.

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

Load the product data via attribute

We can load the product data through the product attribute. We can load the product data through product’s sky by using the loadByAttribute and passing the two parameters that is ‘sku’, and $_sku variable which contains the product’s sku value.

$_sku = 'Tshirt-b-m';
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_sku);

Load the product’s categories

Get all the categories data from the product

$_product = Mage::getModel('catalog/product');
$_categories = $_product->getCategoryCollection();

echo '<pre>';


Category model

This function will return all the category information in the database.

$_category = Mage::getModel('catalog/category');

Load the category via category id

We can get the particular category information by loading the category id

$_catId = 24;
$_category = Mage::getModel('catalog/category')->load($_catId);

Use the get prefix to fetch the data

We can use the get prefix to fetch the data.

$_catId = 24;
$_category = Mage::getModel('catalog/category')->load($_catId);
echo $_category->getName();
echo $_category->getUrl();
echo $_category->getDescription();

Load all the product data by category

$cat_id = 24;
$_category = Mage::getModel('catalog/category')->load($cat_id);
$_products = $_category->getProductCollection();
echo '<pre>';


Get the get/post method

The first line gets all the get/post data. The second line only get the particular field named as “name”.



Check Customer Session and get data

This code will check if the customer is logged in or not. If it’s logged in then its fetches the customer data.

if(Mage::getSingleton('customer/session')->isLoggedIn()) {
    //logged in        
    $_customer = Mage::getSingleton('customer/session')->getCustomer();
else {
    // not logged in
$customer = Mage::getSingleton('customer/session')->getCustomer();

Flash session

This function will store the message in the session and displays on the next page as a notification.

Mage::getSingleton('core/session')->addSuccess('Success message');
Mage::getSingleton('core/session')->addNotice('Notice message');
Mage::getSingleton('core/session')->addWarning('Warning message');
Mage::getSingleton('core/session')->addError('Error message');

This message will work only if another redirect page contains one line of code

<?php echo $this->getMessagesBlock()->toHtml() ?>

Setting own session variable and data

The prefix set will store the key and value on the session. The get prefix will get the data. The awesome is the key of the session.

Mage::getSingleton('core/session')->setAwesome('This is awesome open source platform');
echo Mage::getSingleton('core/session')->getAwesome();


CMS Block

This functionality will fetch all the CMS block data that was created on the cms page.

$collection = Mage::getModel('cms/block')
$collection->setOrder('identifier', 'DESC');

Category Static Block

This functionality will listed out static block name, identifier, and $content information which was select on the category page.

$cat = Mage::getModel('catalog/category')->load(13);

$mode  = $cat->getDisplayMode();
//display mode = PAGE means, that category has a static block
if($mode == 'PAGE' || $mode == 'PAGE_AND_PRODUCTS'){
    //get static block id
    $page = $cat->getLandingPage();

    //cms block
    $cms_block = Mage::getModel('cms/block')->load($page);

    //retrieve cms block data
    $title = $cms_block->getTitle(); // title of cms block
    $identifier = $cms_block->getIdentifier(); //identifier for that cms block
    $content = $cms_block->getContent(); //get entire content of cms block


// second method

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$page = $category->getLandingPage();
//cms block
$cms_block = Mage::getModel('cms/block')->load($page);

//retrieve cms block data
echo $title = $cms_block->getTitle(); // title of cms block
echo $identifier = $cms_block->getIdentifier(); //identifier for that cms block
echo $content = $cms_block->getContent(); //get entire content of cms block


Customer Model

This code will get all the customer data

$_customer = Mage::getModel('customer/customer');

echo '<pre>';

Get the customer data via customer id

This function will fetch specific customer data by using the customer id

$_customerId = 12;
$_customer = Mage::getModel('customer/customer')->load($_customerId);

echo $_customer->getEmail()
// or 
$data = $customer->getData();
echo $data["email"];

Leave a Reply

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