1

I'd like to add a specific body class when a user login to my Magento 2 store. My goal is to add a class depending on customer group, so I can show/hide some infos easily via CSS.

Currently I can't solve this, please, could you give me some advice?

Thanks in advance

1 Answer 1

0

You can achieve this from javascript.

What you can do is in a template file it can be header.phtml file or you can add the new one get the session of the customer and add the class to the body tag from the javascript checking the required info from the customer session.

Below is the same code that you could use on the template file.

<?php 
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of\Magento\Framework\App\ObjectManager
$customerSession = $_objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()){

    //you can get your customer group information here
    if($condition_matches)

    //set the flag if you condition matches
        $flag = 1;
}    

?>
<script type="text/javascript">
    require([ 'jquery'], function($){ 
    jQuery(document).ready(function(){
        var flag = <?php echo $flag; ?>;
        if(flag=="1"){
            //add the class to the body tag using addClass method.
        }

    });
});
</script>

It is not the complete working code but it will git you hint how you could proceed.

Hope it helps.Cheers!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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