Magento If Statements

Magento can create a few puzzling question along the way and hopefully some of this information will help clear that up!

In my personal experience I’ve used “if-statements” in Magento a great deal to keep the structure intact while still have a very high level of control.

Here are a few snippets that will help you along the way!

1 – If is Home Page

<?php if(Mage::getBlockSingleton('page/html_header')->getIsHomePage()): ?>
//do stuff //content //etc
<?php else: ?>
//do stuff //content //etc
<?php endif; ?>

2 – If is a Specifc Page

<?php if(Mage::getSingleton('cms/page')->getPageId() == 'PAGE ID GOES HERE'  && Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms') : ?>

//make stuff happen!

<?php elseif(Mage::getSingleton('cms/page')->getPageId() == 'PAGE ID GOES HERE'  && Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms') : ?>

//more other stuff happen!

<?php else: ?>

//or do this

<?php endif; ?>

3- if is a specific category

<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
<?php if($category->getId()==CATEGORY ID GOES HERE): ?>

//do stuff content etc

<?php elseif($category->getId()==CATEGORY ID GOES HERE): ?>

//DO STUFF etc

<?php else: ?>

//more stuff

<?php endif; ?>

4- if store ID / Get Store ID

<?php if (Mage::app()->getStore()->getId() == 'yourStoreID'){ ?>

 

//insert your goodies

<?php } else { ?>

//other goodies

<?php } ?>

5 – if store Code / Get Store Code

<?php if (Mage::app()->getStore()->getCode() == 'yourStoreCode'){ ?>

//HTML or JS Code Here

<?php } else { ?>

//other Goodies

<?php } ?>

And this would end the start of my series on “Magento If-Statements” enjoy!