We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    In some cases, you will need to add a class to the body tag of your Magento stores. To process it, you’ll need to set-up an event observer for the layout_render_before event.

    1. Create file events.xml on app/code/Magemonkeys/Addbodyclass/etc/frontend

    <?xml version="1.0" ?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
      <event name="layout_render_before">
         <observer instance="MagemonkeysAddbodyclassObserverFrontendLayoutLoadBefore" name="add_bodystorecode_class_observer"/>
      </event>
    </config>

    2. Create file LoadBefore.php on app/code/Magemonkeys/Addbodyclass/Observer/Frontend/Layout

    <?php
    namespace MagemonkeysAddbodyclassObserverFrontendLayout;
     
    use MagentoFrameworkEventObserver;
    use MagentoFrameworkViewPageConfig;
    use MagentoStoreModelStoreManagerInterface; 
    
    class LoadBefore implements MagentoFrameworkEventObserverInterface
    {
       protected $storeManager;
    
       public function __construct(
           Config $config,
           StoreManagerInterface $storeManager
       ) {
           $this->config = $config;
           $this->storeManager = $storeManager;
       }
     
       public function execute(Observer $observer)
       {
           $store = $this->storeManager->getStore();
           $storeCode = $store->getCode();
           $websiteCode = $store->getWebsite()->getCode();
           $this->config->addBodyClass($storeCode);
           $this->config->addBodyClass($websiteCode);
       }
    }

     

     

    field_5bfb909c5ccae

      Get a Free Quote