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:
if(!Mage::getSingleton('customer/session')->isLoggedIn())
{
$this->_redirect('customer/account/login/');
}
2. Get current site url:
Mage::getUrl();
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:
$this->formatPrice(10);
7. Call a .phtml explicitly:
echo $this->getLayout()->createBlock('sales/order_recent')->setTemplate('sales/order/recent.phtml')->toHtml();
8. Get Currency Code:
Mage::app()->getStore()->getCurrentCurrencyCode();
9. Get Currency Symbol:
Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();
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;
umask(0);
Mage::app();
// 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:
<?php
print_r(get_class_methods(get_class($this)));
?>
or
<?php
print_r($this->debug());
?>
or
<?php
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:
<?php
$currentCategory = Mage::registry('current_category');
?>
16. Add product to cart through querystring:
http://www.mydomain.com/checkout/cart/add?product=[id]&qty=[qty]
http://www.mydomain.com/checkout/cart/add?product=[id]&qty=[qty]&options[id]=[value]
7 comments
Thanks for code snippets. I'm sure that I might use some of them one day.
Magento E-commerce software brings a standardized management of your in terms of sales, inventory and customer management.
Magento helps store / busniness owner to broaden their marketing strategy as well as widen their market place - Magento Tips
Nice collection & more helpful tips.
Thanks
Very Nice and very Help full site..Thanks
Really very nice tips for magento developer.Thanx
good post
Very Awesome and very Help complete website..Thanks
Magento Developer
We would love to hear from you...