How to add a class in body tag
Create di.xml file
Create plugin file Page.php
Today we will create plugin to add custom CSS class in body tag.
Path : app/code/Nilesh/Helpers/etc/frontend/di.xml
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <type name="Magento\Framework\View\Result\Page"> <plugin name="addstoreidtobodytag" type="Nilesh\Helpers\Plugin\Result\Page"/> </type> </config>
Path : app/code/Nilesh/Helpers/Plugin/Result/Page.php
<?php namespace Nilesh\Helpers\Plugin\Result; use Magento\Framework\App\ResponseInterface; class Page { private $context; protected $_storeManager; public function __construct( \Magento\Framework\View\Element\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager ) { $this->context = $context; $this->_storeManager = $storeManager; } public function beforeRenderResult( \Magento\Framework\View\Result\Page $subject, ResponseInterface $response ){ $storeId = $this->_storeManager->getStore()->getId(); $subject->getConfig()->addBodyClass('store-'.$storeId); return [$response]; } }
Happy Coding – Nilesh Dubey
Command : php bin/magento setup:upgrade
Command: php bin/magento setup:static-content:deploy -f
Command : php bin/magento c:f
You all are the semicolon to my statements; Please support me; Thanks