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
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.
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.
-
-
-
-
-
can you just write the if/else statement in php for me please with an example <div>?– CollinApr 29, 2016 at 7:11
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 ?>
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;?>