Select Page

Magento Advanced – Conditional Statements (if-else) on Website View

by | Mar 26, 2016 | Magento Advice | 0 comments

Magento Conditional Statement – on Website / Store View

Magento is one of the most versatile and feature rich e-commerce platforms available with the ability to manage multiple websites under 1 installation, yet it has a very complex programming structure and sometimes you need to think outside of the box to avoid creating a large amount of unnecessary work for yourself.

Recently one of my clients required Adwords conversion tracking to be set up for his Magento multi-store. The only issue is that this client is running the same template (with an alternative skin) on both of these websites.

The challenge was to call the correct conversion code for each website without creating another child theme. I achieved this by using a PHP conditional statement using the website / store view code.

Store View

Using your Store ID

<?php if (Mage::app()->getStore()->getId() == 'StoreID'){ ?>
//Insert required HTML, PHP or JS here
<?php } else { ?>
//Insert required HTML, PHP or JS here
<?php } ?>

Using your Store Code

<?php if (Mage::app()->getStore()->getCode() == 'yourStoreCode'){ ?>
//Insert required HTML, PHP or JS here
<?php } else { ?>
//Insert required HTML, PHP or JS here
<?php } ?>

Website View

Using your Website ID

<?php if (Mage::app()->getWebsite()->getId() == 'yourWebsiteID'){ ?>
//HTML or JS Code Here
<?php } else { ?>
//HTML or JS Code Here
<?php } ?>

Using your Website Code

<?php if (Mage::app()->getWebsite()->getCode() == 'yourWebsiteCode'){ ?>
//HTML or JS Code Here
<?php } else { ?>
//HTML or JS Code Here
<?php } ?>

Getting your Store/Website – Code or ID

From the Magento Dashboard, navigate to:
System > Manage Stores
Under ‘Website Name’ column, click your website or store name.
Get the website value in ‘Code*’ field

Magento Website Code ID

Conor Tomkins

Conor Tomkins

Senior PHP Developer

Working with PHP applications since the age of 17. I have a wide range of knowledge on most PHP open source platforms. I must admit, my favorites are Magento & Wordpress!

Still need answers?