Magento – Determine Page Type and Page

magento-1.7

I have a new job where I strictly work on Magento everyday now. I have no prior experience with Magento so it's been a challenge to say the least so far!

Let me explain briefly the extension we are building.

It will allow us to show a Popup/Modal window. The Modal's HTML content will be stored in
a Static Block


ADMIN PAGE

So the Admin panel consist of a new Tab under System/Confuration/NEW TAB

The admin panel allows you to select in a drop down list on that NEW TAB PAGE the default Static Block to use for these sections…

HOME PAGE or HOME PAGE w/Items in Shopping Cart
ALL CATEGORY PAGES or ALL CATEGORY PAGES w/Items in Shopping Cart
ALL PRODUCTS PAGES or ALL PRODUCTS PAGES w/Items in Shopping Cart
ALL CMS PAGES or ALL CMS PAGES w/Items in Shopping Cart
SHOPPING CART PAGE or SHOPPING CART PAGE w/Items in Shopping Cart


DEFAULT OVER-RIDES

Next the actual CMS, CATEGORY, and PRODUCT pages all allow the same settings that the Admin Panel page has, except they allow you to set a STATIC BLOCK on a per-item basis and over-ride the default setting that we had in the admin settings above.


So far we have the admin side of the extension done. We have all the admin tabs for each section… product, category, CMS, and Configuration page.

Next step will be to add on to the same extension and make a Static Block insert into the page. This Static Block will be responsible for… Showing the correct Static Block on each page based on the settings that were set in the above mentioned areas…..

  • Determining the page we are on, and the page type (cms, product, home, category)
  • It will then have to read all the custom settings that were set in the above mentioned areas.
  • If it is determined we are on a Category page, we will then have to get the actual specifiv Category we are on. For example Category 3. We will then see if there is a specific setting for that specific category. If it says Enabled we will then look for which Static Block is saved for that category and show it in our main Static Block on the page!
  • If the above Specific Category is set as Disabled for our Modal Window then we will look higher up in the more global settings for the Modal and see if a Modal is Enabled for all categories. If yes, then we will see which Static Block is set to be shown to all categories.
  • In addition to all this, it will do this process for each of out main page types (product, categories, cms, home, cart)
  • We will also have to check if the shopping cart is empty or not. IF not empty…then we will have to take that into consideration above when wqe are checking which Static Block to show for that page as each setting allows for a Static Block to be set for empty cart and 1 for not empty cart! (I know this is a massive hard and large undertaking but we have the admin side done mostly. Just need to do the part that takes all those into account in determining which Static Block to show on which particular page.

So to get me in the right direction I have a couple of smaller questions…

  • How can I inside my Static Block code (the PHP part) determine which TYPE of PAGE I am on. Like I mentioned I need to know if I am on…. HOME PAGE, a CMS PAGE, a CATEGORY PAGE, a PRODUCT PAGE, or the SHOPPING CART PAGE ?
  • In addition to the PAGE TYPE I need to get the actual page. So if I am on Product id 1234 then I need to be able to get the setting (static block set for that page) for that particular page.

I know this has been a long and possibly confusing post. If you made it this far, thank you for taking your time to read this =)

If you can assist with my question, each little chunk will get me closer to the end goal


Below is a Flow chart I made of how my module will need to work to determine which static block to insert into the page

enter image description here

Best Answer

In addition to what @benmarks answered:

Here is a little code to check some of the conditions you mentioned:

// Check if it's a CMS page:
$page = Mage::getSingleton('cms/page');
if ($page->getId()) {
    // The current page is a CMS page

    if ($page->getIdentifier() == Mage::getStoreConfig('web/default/cms_home_page')) {
        // The current page is the CMS home page
    }
}

$product = Mage::registry('current_product');
$category = Mage::registry('current_category');
if ($product && $product->getId()) {
    // The current page is a product page.
    // If you only want the main product detail page, also check for 
    // Mage::app()->getFrontController()->getAction()->getFullActionName() == 'catalog_product_view'
    // Be aware that a current_product and a current_category can be set at the same time.
    // In that case the visitor is viewing a product in a category.
} elseif ($category && $category->getId()) {
    // The current page is a category page
    // If you only want the category list page, also check for 
    // Mage::app()->getFrontController()->getAction()->getFullActionName() == 'catalog_category_view'
}

// Check for cart page
if (Mage::app()->getFrontController()->getAction()->getFullActionName() == 'checkout_cart_index') {
    // The current page is the cart
}

To check if the cart is empty or not use

$isEmpty = Mage::getSingleton('checkout/session')->getQuote()->getItemsCount() > 0

Also you asked if you can check the current page inside a CMS block.
That probably is not a good approach (you would have to include custom blocks, which would defeat the purpose of using CMS blocks in the first page).
Instead it's better to check the current page first, and then choose the CMS block to display based on that.

EDIT: fixed Mage::app()->getRequest()->getFullActionName() to be Mage::app()->getFrontController()->getAction()->getFullActionName() as mentioned by @Joseph in the comments. Thanks!

Related Topic