Magento Cheat Sheet – Frontend template file snippets

Frontend template file snippets

Following are some tips and tricks for you all to go through the rough and tough Magento jobs. Just making things easier. I’m gathering things which I have used at times and from the internet over times to build these posts. Please excuse me, I already know the similar many info are scattered all around my other posts.


Hard coded content in templates (for translations)

$text = $this->__('Content');


$float_value to currency format

$amount = Mage::helper('core')->currency($float_value, true, false);


Get URLs

$url = $this->getSkinUrl('images/file.jpg'); // Get skin asset URL
$url = Mage::helper('core/url')->getHomeUrl(); // Get home URL
$url = Mage::helper('core/url')->getCurrentUrl(); // Get current URL
$url = $this->getUrl('page.html'); // Get specific page URL


Get current category

$_category = Mage::getModel('catalog/layer')->getCurrentCategory();


Load category by ID

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


Load product by ID

$_product = Mage::getModel('catalog/product')->load($product_id);


Load product by SKU

$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $product_sku);


Load configurable product’s children

$_child_products = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $_product);


Get resized product image URL (Use in catalog/product_view_media blocks only)

$this->helper('catalog/image')->init($_product, $_image_attribute_name)
->keepFrame(false) // Remove white border around images
->constrainOnly(true) // Don't enlarge further than original size
->keepAspectRatio(true) // Don't crop image
->resize($_image_width, $_image_height); // Resize to dimensions provided


Check if customer logged in

$logged_in = Mage::getSingleton('customer/session')->isLoggedIn();


Get a clean string with no symbols or spaces

$_cleaned = Mage::getModel('catalog/product_url')->formatUrlKey($_dirty);