Quick tips for Magento Developers

Share on FacebookTweet about this on TwitterShare on Google+Share on RedditShare on LinkedIn

Here I am presenting a list of some useful code snippets for Magento developers that are required here and there in code development. These are the most frequently used code snippets that can sometimes eat up a lot of your time. So here is the collection…

1. Check if customer is logged in:


2. Get current site url:


3. Get Category URL:

$categoryUrl = Mage::getModel('catalog/category')->load($categoryId)->getUrl(); 

4. Get product URL:

$productId = 1;
$product = Mage::getModel('catalog/product')->load($productId);
$path = Mage::getUrl().$product->getUrlPath();

5. Get Product image path:

$productId = 1;
$product = Mage::getModel('catalog/product')->load($productId);
$path = Mage::helper('catalog/image')->init($product, 'image')->resize(75, 75);

6. Format a number as price:


7. Call a .phtml explicitly:

echo $this->getLayout()->createBlock('sales/order_recent')->setTemplate('sales/order/recent.phtml')->toHtml();

8. Get Currency Code:


9. Get Currency Symbol:


10. Get Customer Shipping/Billing Address:

$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);

11. Get all products in the cart:

$mageFilename = 'app/Mage.php';
require_once $mageFilename;

// Secret Sauce - Initializes the Session for the FRONTEND
// Magento uses different sessions for 'frontend' and 'adminhtml'
Mage::getSingleton('core/session', array('name'=>'frontend'));

$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllItems() as $item)
$productid = $item->getProductId();
$name = $item->getName();
$qty = $item->getQty();

echo "Product Id-".$productid.', Name-'.$name. ', Quantity-'.$qty;

12. Find which class is loaded in current template:

echo Zend_Debug::dump($this);

13. Get Current theme folder:

<?php echo $this->getSkinUrl('') ?>

14. Get total price of items in the cart:

<?php echo $this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal()); ?>

15. Get the current category:

$currentCategory = Mage::registry('current_category');

16. Add product to cart through querystring:


Share on FacebookTweet about this on TwitterShare on Google+Share on RedditShare on LinkedIn

About Arvind Bhardwaj

7 thoughts on “Quick tips for Magento Developers

Leave a Reply

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