A
A
Andrey2021-10-06 20:50:23
opencart
Andrey, 2021-10-06 20:50:23

How to set body class in opencart 3?

How to make body be assigned a different class on different pages?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
opencartbot, 2021-10-11
@klekovkinandrey

In the controller common/header.php add the formation of the class name:

if (isset($this->request->get['route'])) {
      if (isset($this->request->get['product_id'])) {
        $class = '-' . $this->request->get['product_id'];
      } elseif (isset($this->request->get['path'])) {
        $class = '-' . $this->request->get['path'];
      } elseif (isset($this->request->get['manufacturer_id'])) {
        $class = '-' . $this->request->get['manufacturer_id'];
      } elseif (isset($this->request->get['information_id'])) {
        $class = '-' . $this->request->get['information_id'];
      } else {
        $class = '';
      }

      $data['class'] = str_replace('/', '-', $this->request->get['route']) . $class;
    } else {
      $data['class'] = 'common-home';
    }

Now in the common/header.twig template, you can add the output of the class to the body:
<body{% if class %} class="{{ class }}"{% endif %}>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question