Magento Code Snippets

Get all the product attributes which are searchable

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

$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

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 or not

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

Check if Module is installed or not

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

Get current package and frontend theme name

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

Get store contact telephone

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

Get store timezone

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

Get the title of CMS static block

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

Get all the children of a block in PHTML

$children = $this->getChild();

Check if this is category page or product page

$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

$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

$crosssell = $product->getCrossSellProducts();
if(count($crosssell)) {
    //product have cross-sell products...

Get all active payment methods

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

Get all active shipping carriers

$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!




Magento top 50 contributors

Magento top 50 contributors