11

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

4
  • 2
    You do realize that you've effectively recreated functionality which exists in the Mage_Widget module, right?
    – benmarks
    Commented Jul 11, 2013 at 2:52
  • Ditto what @benmarks said. IMO you'll be better off trying to take advantage of widgets and going from there.
    – davidalger
    Commented Jul 11, 2013 at 3:01
  • @benmarks Looking at the flow chart I have added to my question, would you say that it is still achievable 100% with the Mage_Widget ? I need to look into that as well
    – JasonDavis
    Commented Jul 12, 2013 at 8:17
  • No. Core widget blocks do not understand combinations such as those indicated in your chart. You are going to want some block logic for this.
    – benmarks
    Commented Jul 12, 2013 at 9:37

3 Answers 3

12

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!

4
  • Thank you so much this is going to really help get me started!
    – JasonDavis
    Commented Jul 11, 2013 at 6:13
  • 1
    Just to note: using Mage::app()->getRequest()->getFullActionName() in a cart template results in a fatal error or at least it did for me on 1.7.0.2. $this->getAction()->getFullActionName() worked perfectly! Commented Dec 12, 2013 at 14:06
  • Uh, yeah, you are right on track. My bad! Thanks!
    – Vinai
    Commented Dec 12, 2013 at 19:30
  • FYI, Mage::getSingleton('cms/page')->getId() gives null value in controller_action_predispatch event.
    – MagePsycho
    Commented Nov 15, 2014 at 10:10
7

Lots of approaches you could take. I'd recommend adding your block via the <default> layout update handle, which would make it available on all pages. In your block's _prepareLayout() method, you can determine the scope by getting the current full action name from the request object ($this->getRequest()->getFullActionName()), and using that to determine context.

Products and categories will be registered in the registry. Your block could also look at the layout update handles, which will include product ID or category ID handles.

3

To detect the home page, you need to use the identifier string (e.g. "home") to match against the config value, rather than the numeric id:

if ($page->getIdentifier() == Mage::getStoreConfig('web/default/cms_home_page')) {
    // Home page
}

Here are some others:

$fullActionName = Mage::app()->getFrontController()->getAction()->getFullActionName();
if ('checkout_onepage_index' == $fullActionName) {
    // Checkout
}

if ('checkout_onepage_success' == $fullActionName) {
    // Checkout success
}

if ('customer_account_login' == $fullActionName) {
    // Login
}

if ('customer_account_logoutSuccess' == $fullActionName) {
    // Logout success
}

if ('customer_account_create' == $fullActionName) {
    // Customer account create
}
if (0 === strpos($fullActionName, 'catalogsearch_')) {
    // Search
}

if (0 === strpos($fullActionName, 'customer_account_')
 || 0 === strpos($fullActionName, 'customer_address_')
 || 0 === strpos($fullActionName, 'wishlist_')
 || 0 === strpos($fullActionName, 'newsletter_manage_')
 || 0 === strpos($fullActionName, 'review_customer_')
 || 0 === strpos($fullActionName, 'sales_order_')
) {
    // Customer account page (there are probably others)
}

Thanks for the helpful info everyone!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.