2

I have a website with 2 stores, one for each language. Magento adds the store name to the body on CMS pages, but not on the rest of the sites. The only setting that is remotely related allows me to add the store code to the url, but this is not what I want.

I googled for an answer and I didn't find what I'm looking for.

Has anyone done this before and found a solution?

1
7

You can do this by event/observer

As peras magento system body class has been set by addBodyClass() of Mage_Page_Block_Html,So on event of core_block_abstract_to_html_before fire an observer and on this observer using addBodyClass() function class Mage_Page_Block_Html,you can add store code body tag.

config.xml code:

<events>
  <core_block_abstract_to_html_before> <!-- identifier of the event we want to catch -->
    <observers>
      <core_block_abstract_to_html_before_handler> <!-- identifier of the event handler -->
        <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
        <class>magento68574/observer</class> <!-- observers class alias -->
        <method>AddClass</method>  <!-- observer's method to be called -->
        <args></args> <!-- additional arguments passed to observer -->
      </core_block_abstract_to_html_before_handler>
    </observers>
  </core_block_abstract_to_html_before>
</events>

Observer code:

<?php
class Stackexchange_Magento68574_Model_Observer
{

            public function AddClass(Varien_Event_Observer $observer)
            {
                 $block = $observer->getBlock();
            //echo get_class($block)."--->" .get_class( $block->getParentBlock())."<br/>";
                if (get_class($block) == 'Mage_Page_Block_Html') {

                    $block->addBodyClass(Mage::app()->getStore()->getCode());

                    }
            }

}

Full Module:

As per as your request, i have put full module

Step1: Observer path app/code/local/Stackexchange/Magento68574/Model/Observer.php

code:

<?php
class Stackexchange_Magento68574_Model_Observer
{

            public function AddClass(Varien_Event_Observer $observer)
            {
                 $block = $observer->getBlock();
            //echo get_class($block)."--->" .get_class( $block->getParentBlock())."<br/>";
                if (get_class($block) == 'Mage_Page_Block_Html') {

                    $block->addBodyClass(Mage::app()->getStore()->getCode());

                    }
            }

}

Step2: config path app/code/local/Stackexchange/Magento68574/etc/config.xml

code:

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento68574>
      <version>0.1.0</version>
    </Stackexchange_Magento68574>
  </modules>
  <global>
    <models>
      <magento68574>
        <class>Stackexchange_Magento68574_Model</class>
      </magento68574>
    </models>
    <events>
      <core_block_abstract_to_html_before> <!-- identifier of the event we want to catch -->
        <observers>
          <core_block_abstract_to_html_before_handler> <!-- identifier of the event handler -->
            <type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
            <class>magento68574/observer</class> <!-- observers class alias -->
            <method>AddClass</method>  <!-- observer's method to be called -->
          </core_block_abstract_to_html_before_handler>
        </observers>
      </core_block_abstract_to_html_before>
    </events>
  </global>
</config> 

Step3: module file: app/etc/modules/Stackexchange_Magento68574.xml

code

<?xml version="1.0"?>
<config>
  <modules>
    <Stackexchange_Magento68574>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Stackexchange_Magento68574>
  </modules>
</config>
5
  • Thanks for your help @Amit. Which files should I use this code in? Please forgive my ignorance, I have very little experience with layout updated in magento. May 20 '15 at 17:09
  • Thanks again for your very complete answer. I have created the necessary files with the content you have provided in the specified folder structure, however no new class is added to the body. I have checked that the new extension shows up under System > Configuration > Advanced and that it's HTML output is enabled. May 20 '15 at 18:36
  • I have noticed that you use alternatively "Magento68574" and "Magento65569" in your code, is this correct? May 20 '15 at 18:42
  • edited.it was mistake...please ckd now
    – Amit Bera
    May 20 '15 at 18:51
  • Thank you, I have marked your answer as correct since it does exactly what I need. May 20 '15 at 19:32
2

The most simple solution is:

Go to

app/design/frontend/[yourtheme]/[yourlayout]/page/[mainlayout files].phtml

[mainlayout files] = 2columns-left.phtml, 2columns-right.phtml, 1column.phtml and 3columns.phtml

Find the <body> tag and add:

echo Mage::app()->getStore()->getCode()

The total bodyclass should look something like:

<body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass(). ' store-' .Mage::app()->getStore()->getCode().'"':'' ?>>

Above code outputs the following to the front-end:

<body class=" catalog-category-view categorypath category store-en">

The store-en is the class added for a store with storecode 'en'. This wil be the storecode you entered in the backend under system/Manage stores

1

You should be able to do this avoiding event/observer completely.

Add this in layout XML:

<default>
    <action method="addBodyClass" block="root">
         <store_code helper="YourNamespace_YourModule/getStoreCodeName" />
    </action>
</default>

And this helper method in YourNamespace_YourModule helper class:

public function getStoreCodeName()
{
  return Mage::app()->getStore()->getCode();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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