Finally Article on Magento after very long time. Last article which I have posted about magento is about Magento EAV Database structure. In that article I have explained the reason for the flexibility Magento.
In this article I will talk about some tips and tricks which will be much helpful when you are dealing with the Theme Integration.
[gads]
Reason For this Article
In this article I am going to share tips for Magento with which we can identify the page on which we are; just like home page, category page, product page, etc.
Main to reason for this article is that; it will be very much helpful when you want to load some content conditionally. Like some js files need to be loaded on certain pages only. Yes we can manage this using XML file as well in magento but I am just taking this as an example. But the thing is below conditions will be useful when you really need it.
So let’s start with conditions for different pages. We can use this code in .phtml file.
Detect Home Page – Magento
Method 1 (For All Version of Magento):
[cc lang=”php”]
if($this->getUrl(”) == $this->getUrl(‘*/*/*’, array(‘_current’=>true, ‘_use_rewrite’=>true)))
{
// Home page
}
[/cc]
Method 2 (For Magento 1.5 and Above):
[cc lang=”php”]
if($this->getIsHomePage())
{
// Home page
}
[/cc]
[gads]
Detect Category Page – Magento
[cc lang=”php”]
if (Mage::registry(‘current_category’))
{
// category page
}
[/cc]
Now see how we can get ID and Name of the category if current page is category page.
[cc lang=”php”]
if (Mage::registry(‘current_category’))
{
// Category Name
echo Mage::registry(‘current_category’)->getName();
// Category ID
echo Mage::registry(‘current_category’)->getId();
}
[/cc]
Detect CMS Page – Magento
[cc lang=”php”]
if(Mage::app()->getFrontController()->getRequest()->getRouteName() == ‘cms’)
{
// CMS page
}
[/cc]
Get CMS page name if current one is the CMS page.
[cc lang=”php”]
if(Mage::app()->getFrontController()->getRequest()->getRouteName() == ‘cms’)
{
echo Mage::getSingleton(‘cms/page’)->getIdentifier();
}
[/cc]
Detect Product Detail Page – Magento
[cc lang=”php”]
if(Mage::registry(‘current_product’))
{
// Product detail page
}
[/cc]
Detect Configure Product Page – Magento
[cc lang=”php”]
if(Mage::app()->getFrontController()->getRequest()->getRequestedActionName() == ‘configure’)
{
// Product Configuration page
}
[/cc]
Detect Cart Page – Magento
[cc lang=”php”]
$request = $this->getRequest();
$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();
if($module == ‘checkout’ && $controller == ‘cart’ && $action == ‘index’)
{
//Cart Page
}
[/cc]
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.
If you don’t want to miss any upcoming article then subscribe to our RSS Feed via mail, Like us on Facebook or Follow us on Twitter. If you do so, I am sure you will never miss any article/freebies or updates from us.
[…] I comes with one more quick tip for Magento, earlier we have seen tips to detect the pages in Magento. In this article I would like to share 4 ways with which you can get home page URL in […]
Method 2 does not work in magento 8.1
We use
$route = Mage::app()->getFrontController()->getRequest()->getRouteName();
$action = Mage::app()->getFrontController()->getRequest()->getActionName();
if($route == ‘cms’ && $action == ‘index’):
echo $this->getChildHtml(‘shopper_footer_partners’);
endif;