Magento Code Snippets

Get all the product attributes which are searchable

1
select a.attribute_code from eav_attribute a inner join catalog_eav_attribute b on a.attribute_id = b.attribute_id where b.is_searchable = 1;

Get product type from product SKU or ID

1
2
3
$product = Mage::getModel('catalog/product')->load($product_id);
//$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$product_sku);
$productType = $product->getTypeId(); //gives simple, configurable, bundled, grouped, etc..

Check whether request is coming from frontend or backend

1
2
3
4
5
if(Mage::app()->getStore()->isAdmin() && Mage::getDesign()->getArea() == 'adminhtml'){
        //request is from backend/admin
} else {
        //request is NOT from backend/admin
}

Check if there are any items in cart or not

1
$totalItemsInCart = Mage::helper('checkout/cart')->getItemsCount();

Check if Module is installed or not

1
$moduleExists = Mage::getConfig()->getNode('modules/Namespace_Module'); //returns YES or NO

Get current package and frontend theme name

1
2
$currentPackage = Mage::getSingleton('core/design_package')->getPackageName();
$currentTheme = Mage::getSingleton('core/design_package')->getTheme('frontend');

Get store contact telephone

1
$storePhone = Mage::getStoreConfig('general/store_information/phone');

Get store timezone

1
$storeTimezone = Mage::getStoreConfig('general/locale/timezone');

Get the title of CMS static block

1
2
$block = Mage::getModel('cms/block')->load($blockID);
echo $block->getTitle();

Get all the children of a block in PHTML

1
2
$children = $this->getChild();
Mage::log($children->getData());

Check if this is category page or product page

1
2
3
4
5
6
$req = Mage::app()->getFrontController()->getRequest()->getControllerName();
if($req == 'category') {
    $cat = Mage::registry('current_category');
} elseif($req == 'product') {
    $prod = Mage::registry('current_product');
}

Get first and/or last item from collection

1
2
$firstItem = $collection->getFirstItem(); Mage::log($firstItem->getData());
$lastItem = $collection->getLastItem(); Mage::log($lastItem->getData());

Check if there are any cross-sell products attached to a product

1
2
3
4
$crosssell = $product->getCrossSellProducts();
if(count($crosssell)) {
    //product have cross-sell products...
}

Get all active payment methods

1
$activePayMethods = Mage::getModel('payment/config')->getActiveMethods();

Get all active shipping carriers

1
$activeCarriers = Mage::getSingleton('shipping/config')->getActiveCarriers();

Error: Index process is working now. Please try run this process later
This error generally comes when Magento was re-indexing one of the indexes and it failed in between. The solution is to remove the relevant index lock at: magento/var/locks/index_process*.lock

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications