1

I want to show certain content consisting within a <div> within the website header, so within header.phtml. I want to show this on all pages, except on the checkout/cart page. How can I achieve this?

4 Answers 4

1

I'm missing a pure layout XML solution between all the answers here. Comparing URL's (for instance against Mage::getURL('checkout/onepage')) might give you problems when something is out of the ordinary in the URL (like a SID is being added, a session ID is being added or there are GET parameters in the current URL).

Here's how you can do this in a clean way using layout XML. Add this to your local.xml or your custom module's layout XML file.

Default (all pages):

<layout>
    <default>
        <reference name="header">
            <block type="core/template" name="headerdiv" template="page/html/headerdiv.phtml" />
        </reference>
    </default>

   <!-- [...] -->

</layout>

Now add another section removing the headerdiv block on checkout_onepage:

<checkout_onepage_index>
    <remove name="headerdiv" />
</checkout_onepage_index>

You can repeat this for other page handles, for instance the homepage (<cms_index_index>) or the <checkout_onepage_success>:

<cms_index_index>
    <remove name="headerdiv" />
</cms_index_index>

You should add a call to getChildHtml() in your header.phtml at the point you want to have the <div> included:

<?php echo $this->getChildHtml('headerdiv'); ?>

Of course the template page/html/headerdiv.phtml, residing in your theme, would contain the actual <div> you want to show.

0

You can get checkout page url by:

$url1=Mage::getURL('checkout/onepage')

And current url by:

$url2=Mage::helper('core/url')->getCurrentUrl()

Now you can compare if these two are same like:

if($url1==$url2) { }

EDIT: Check below code:

<?php
$url1=Mage::getURL('checkout/onepage');
$url2=Mage::helper('core/url')->getCurrentUrl();
if($url1==$url2) { 
?>
    <style type="text/css">
    .class{display:none;}
    </style>
<?php
}
?>

.class is the class of your div.

6
  • What does that look like when I add it to the PHTML file?
    – Collin
    Apr 29, 2016 at 7:00
  • you may add some css or js to hide the div in condition Apr 29, 2016 at 7:03
  • do I add all 3 lines of code?
    – Collin
    Apr 29, 2016 at 7:04
  • @Collin yes sure. Apr 29, 2016 at 7:08
  • can you just write the if/else statement in php for me please with an example <div>?
    – Collin
    Apr 29, 2016 at 7:11
0

Here you go

    <?php if(Mage::getURL('checkout/onepage') == Mage::helper('core/url')->getCurrentUrl()) ?>
        <?php //it is the checkout page ?>
                 <style type="text/css">
                .yourDIV{display:initial;}
                </style>   
       <?php else: ?>
                <style type="text/css">
                .yourDIV{display:none;}
                </style>
        <?php endif ?>
0

add bellow code in header.phtml

<?php if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'checkout'):?>
<?php else:?>
    <div>test</div>
<?php endif;?>

OR

<?php if($this->getIsHomePage()):?>
    <?php echo 'You are in Homepage!';?>    
<?php elseif(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'checkout'):?>
<?php else:?>
    <div>test</div>
<?php endif;?>

OR

<?php $routeName = Mage::app()->getFrontController()->getRequest()->getRouteName(); ?>
<?php if(in_array($routeName, array('checkout', 'cms'))):?>
<?php else:?>
    <div>test</div>
<?php endif;?>

OR

<?php $routeName = Mage::app()->getFrontController()->getRequest()->getRouteName(); ?>
<?php if(($routeName == 'checkout') || $this->getIsHomePage()):?>
<?php else:?>
    <div>test</div>
<?php endif;?>
4
  • It works, thank you. Is it possible to have multiple if conditions? For example checkout and homepage?
    – Collin
    Apr 29, 2016 at 7:43
  • yes it is possible
    – Abdul
    Apr 29, 2016 at 9:16
  • Updated and with multiple if conditions
    – Abdul
    Apr 29, 2016 at 9:28
  • Please consider my answer using layout XML as this is the best practice
    – 7ochem
    Apr 29, 2016 at 23:05

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