문제

Currently, I am working to HTML to Magento 2 conversion and I got the HTML from the designer which have the class in the body so I added it generally add

in default.xml file in my Magento theme

but the main problem is body class is different for other pages

e.g

body class for home page is "home_page_body_class"

and for other pages is "other_page_body_class"

I do not understand how to achieve it.

If anyone has an idea about that please share me.

도움이 되었습니까?

해결책

You can do this by using a plugin.

app/code/Myvendor/Mymodule/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="myModuleResultPage" type="Myvendor\Mymodule\Plugin\Result\Page"/>
</type>    
</config>

app/code/Myvendor/Mymodule/Plugin/Result/Page.php

<?php
namespace Myvendor\Mymodule\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($this->context->getRequest()->getFullActionName() == 'checkout_cart_index'){
           $subject->getConfig()->addBodyClass('my_custom_class');
        }    
        return [$response];
    }
}

Reference: https://www.mexbs.com/magento-blog/adding-a-custom-body-class-to-a-page-in-magento-2/

Hope it helps!!!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top