If we want to know page type ( whether it is a Category, Product, CMS, Cart page or Checkout Success page ) in Magento, the following code may come handy.
<?php
// GET current Category, Product
$category = Mage::registry('current_category');
$product = Mage::registry('current_product');
// WE May use the below also
//$product = Mage::registry('product');
// GET ROUTE information
$route = Mage::app()->getFrontController()
->getRequest()->getRouteName();
// Return controller name
$controller = Mage::app()->getRequest()
->getControllerName();
// Return action name
$action = Mage::app()->getRequest()->getActionName();
if($product != null)
{
// PRODUCT PAGE
// WRITE code for PRODUCT PAGE
}
else if($category != null)
{
// CATEGORY PAGE
// WRITE code for CATEGORY PAGE
}
else
{
// CMS PAges
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
// CART PAGE and ORDER SUCCESS Page
if($route == 'checkout')
{
// Success Page
if($action == 'success')
{
// GET ORDER TOTAL
$orderId = Mage::getSingleton('checkout/session')
->getLastRealOrderId();
$order = Mage::getSingleton('sales/order')
->loadByIncrementId($orderId);
$orderTotal = $order->getGrandTotal();
}
else
// CART PAge
{
// GET Cart Total
$quote = Mage::getModel('checkout/session')
->getQuote();
$quoteData= $quote->getData();
$cartTotal = $quoteData['grand_total'];
}
}
// FIRECHECKOUT PAGE
if($route == 'firecheckout')
{
// IF IT is FIRECHECKOUT Page
}
// Search Result PAGE
if($route == 'catalogsearch')
{
// IF IT is SEARCH RESULT Page
}
// Website HOME page
if($identifier == "home")
{
// IF it is HOME PAGE
}
}
?>
So, In the first section of the program, we are trying to get if it is a CATEGORY or PRODUCT page by calling
$category = Mage::registry('current_category');
$product = Mage::registry('current_product');
We may use $product = Mage::registry('product'); instead of $product = Mage::registry('current_product');
IF it is a Category page, $category would have a non-null value. Similarly, when it is a product page, $product variable would have a non-null value.
To determine CMS and other pages like Cart, Checkout etc, we need to catch the route, controller name and action name. So we use the following ::
$route = Mage::app()->getFrontController()
->getRequest()->getRouteName();
$controller = Mage::app()->getRequest()->getControllerName();
$action = Mage::app()->getRequest()->getActionName();
For CMS pages, we get page ID/Identifier by calling this ::
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
There is another Magento fundtion called getFullActionName() which returns full action name which comprises of route name, controller name and action name.
$fullActionName = Mage::app()->getFrontController()
->getAction()
->getFullActionName();
So, on "Contacts" page, full action name is 'contacts_index_index' where 'contacts' is the front name, first 'index' means 'indexController' controller and second 'index' refers to 'indexAction' function/method defined within that controller PHP file.
Some full action name examples given below. From there, we can easily identify the Front/Route name, Controller Name and Action Name.
checkout_cart_index => Cart Page
checkout_onepage_index => Checkout Page
checkout_onepage_success => Checkout Success Page
customer_account_login => Customer Login
customer_account_logoutSuccess => Customer Logout
customer_account_create => Registration Page
customer_account_index => Customer Dashboard
wishlist_index_index => My Wishlist
cms_page_view => any CMS page
contacts_index_index => Contacts page
catalog_seo_sitemap_category => SiteMap Catalog
catalogsearch_term_popular => Search Term Page
catalogsearch_advanced_index => Advanced Search
Hope this helps.
<?php
// GET current Category, Product
$category = Mage::registry('current_category');
$product = Mage::registry('current_product');
// WE May use the below also
//$product = Mage::registry('product');
// GET ROUTE information
$route = Mage::app()->getFrontController()
->getRequest()->getRouteName();
// Return controller name
$controller = Mage::app()->getRequest()
->getControllerName();
// Return action name
$action = Mage::app()->getRequest()->getActionName();
if($product != null)
{
// PRODUCT PAGE
// WRITE code for PRODUCT PAGE
}
else if($category != null)
{
// CATEGORY PAGE
// WRITE code for CATEGORY PAGE
}
else
{
// CMS PAges
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
// CART PAGE and ORDER SUCCESS Page
if($route == 'checkout')
{
// Success Page
if($action == 'success')
{
// GET ORDER TOTAL
$orderId = Mage::getSingleton('checkout/session')
->getLastRealOrderId();
$order = Mage::getSingleton('sales/order')
->loadByIncrementId($orderId);
$orderTotal = $order->getGrandTotal();
}
else
// CART PAge
{
// GET Cart Total
$quote = Mage::getModel('checkout/session')
->getQuote();
$quoteData= $quote->getData();
$cartTotal = $quoteData['grand_total'];
}
}
// FIRECHECKOUT PAGE
if($route == 'firecheckout')
{
// IF IT is FIRECHECKOUT Page
}
// Search Result PAGE
if($route == 'catalogsearch')
{
// IF IT is SEARCH RESULT Page
}
// Website HOME page
if($identifier == "home")
{
// IF it is HOME PAGE
}
}
?>
So, In the first section of the program, we are trying to get if it is a CATEGORY or PRODUCT page by calling
$category = Mage::registry('current_category');
$product = Mage::registry('current_product');
We may use $product = Mage::registry('product'); instead of $product = Mage::registry('current_product');
IF it is a Category page, $category would have a non-null value. Similarly, when it is a product page, $product variable would have a non-null value.
To determine CMS and other pages like Cart, Checkout etc, we need to catch the route, controller name and action name. So we use the following ::
$route = Mage::app()->getFrontController()
->getRequest()->getRouteName();
$controller = Mage::app()->getRequest()->getControllerName();
$action = Mage::app()->getRequest()->getActionName();
For CMS pages, we get page ID/Identifier by calling this ::
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
There is another Magento fundtion called getFullActionName() which returns full action name which comprises of route name, controller name and action name.
$fullActionName = Mage::app()->getFrontController()
->getAction()
->getFullActionName();
So, on "Contacts" page, full action name is 'contacts_index_index' where 'contacts' is the front name, first 'index' means 'indexController' controller and second 'index' refers to 'indexAction' function/method defined within that controller PHP file.
Some full action name examples given below. From there, we can easily identify the Front/Route name, Controller Name and Action Name.
checkout_cart_index => Cart Page
checkout_onepage_index => Checkout Page
checkout_onepage_success => Checkout Success Page
customer_account_login => Customer Login
customer_account_logoutSuccess => Customer Logout
customer_account_create => Registration Page
customer_account_index => Customer Dashboard
wishlist_index_index => My Wishlist
cms_page_view => any CMS page
contacts_index_index => Contacts page
catalog_seo_sitemap_category => SiteMap Catalog
catalogsearch_term_popular => Search Term Page
catalogsearch_advanced_index => Advanced Search
Hope this helps.
No comments:
Post a Comment