Magento check if cms page, category page & product page

Detect Home Page – Magento
Method 1 (For All Version of Magento):

if($this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true)))
  // Home page

Method 2 (For Magento 1.5 and Above):

  // Home page

Detect Category Page – Magento

if (Mage::registry('current_category')) 
  // category page

Now see how we can get ID and Name of the category if current page is category page.

if (Mage::registry('current_category')) 
  // Category Name
  echo Mage::registry('current_category')->getName();

  // Category ID
  echo Mage::registry('current_category')->getId();

Detect CMS Page – Magento

if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
  // CMS page

Get CMS page name if current one is the CMS page.

if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
  echo Mage::getSingleton('cms/page')->getIdentifier();

Detect Product Detail Page – Magento

  // Product detail page

Detect Configure Product Page – Magento

if(Mage::app()->getFrontController()->getRequest()->getRequestedActionName() == 'configure')
  // Product Configuration page
Detect Cart Page – Magento
 $request = $this->getRequest();
 $module = $request->getModuleName();
 $controller = $request->getControllerName();
 $action = $request->getActionName();
 if($module == 'checkout' && $controller == 'cart' && $action == 'index')
   //Cart Page

I have found above code snippets very useful when I used to work with the Magento. What about you? Consider sharing this article if you find this useful. Let me know your reaction by commenting here.

WordPress Custom Post Type Search

Follow these 3 steps to achieve custom post search.

Lets say your custom post type name is employee_member.

1. Add Below code into function.php

function template_chooser($template)   
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'employee_member' )   
    return locate_template('employee-search.php');  //  redirect to archive-search.php
  return $template;   
add_filter('template_include', 'template_chooser');    

2. Create file `employee-search.php`


        /* Template Name: Custom Search */        
        get_header(); ?>             
        <div class="contentarea">
            <div id="content" class="content_right">  
                     <h3>Search Result for : <?php echo "$s"; ?> </h3>       
                     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>    
                <div id="post-<?php the_ID(); ?>" class="posts">        
                    <h4><a href="<?php the_permalink(); title="<?php the_title();     ?>"><?php the_title(); ?></a><h4>        
                    <p><?php the_exerpt(); ?></p>        
                    <p align="right"><a href="<?php the_permalink(); ?>">Read     More</a></p>    
                    <span class="post-meta"> Post By <?php the_author(); ?>    
                     | Date : <?php echo date('j F Y'); ?></span>    
                    </article><!-- #post -->    
           </div><!-- content -->    
        </div><!-- contentarea -->   
        <?php get_sidebar(); ?>
        <?php get_footer(); ?>

3. Finally create a search form for your custom post type

            <h3>Search Employee Member</h3>
            <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
            <input type="text" name="s" placeholder="Search Products"/>
            <input type="hidden" name="post_type" value="employee_member" /> 
            <input type="submit" alt="Search" value="Search" />

Convert Magento Base Currency to Another Currency

Add currency from Magento Admin -> System -> General -> Currency Setup

$_Baseprice = $_product->getPrice();
$_Currentcurrencycode =Mage::app()->getStore()->getCurrentCurrencyCode();
//This will return USD, EUR e.t.c
//echo $_Currentcurrencycode;
if ($_Currentcurrencycode == 'USD') {$_convertedCurrency = Mage::helper('directory')->currencyConvert($cprice, 'SAR', 'USD');}
else {$_convertedCurrency = Mage::helper('directory')->currencyConvert($cprice, 'SAR', 'SAR');}
//_convertedCurrency will return price without symbol
//echo $_convertedCurrency ;

Magento Connect Package Error

In very rare cases you might see the following message in the console when attempting to install Granify:

CONNECT ERROR: The ‘community’ channel is not installed. Please use the MAGE shell script to install the ‘community’ channel.

The root cause of this is that Magento wasn’t completely setup when installed. You’re most likely to encounter this when the Magento installation is new or there haven’t been any extensions installed yet.

To fix this, have your system administrator SSH into your web server and run the following commands:

chmod 777 mage
./mage mage-setup