How to add body class conditionally in magento 2?

Loading...
How to add body class conditionally  in magento 2?

1.) Create di.xml in at etc\frontend

Using plugin we can add body class at any page.

type="Vendor\Module\Plugin\Result\Page"

2.) In page.php file

namespace Vendor\Module\Plugin\Result;

use Magento\Framework\App\ResponseInterface;

class Page

{

private $context;

public function __construct(

\Magento\Framework\View\Element\Context $context

) {

$this->context = $context;

}

public function beforeRenderResult(

\Magento\Framework\View\Result\Page $subject,

ResponseInterface $response

){

if(condition) {

if($this->context->getRequest()->getFullActionName() == 'checkout_onepage_success') {

$subject->getConfig()->addBodyClass('advance-success-page');

}

}

return [$response];

}

}



Copyright © 2024 Tridhya Tech Limited, Inc. All rights reserved.