D
D
Denis Kuzmin2021-10-18 13:39:23
PHP
Denis Kuzmin, 2021-10-18 13:39:23

How to rewrite module for Opencart (Os2.3)?

The site has a module for displaying categories "Categories_KSL". Please help me rewrite it so that I can display the 3rd level categories.

Controller:

<?php
class ControllerExtensionModuleCategoryKsl extends Controller {
  public function index() {
  
    //Определяем текущую категорию (path из GET запроса)
    if (isset($this->request->get['path'])) {
      $parts = explode('_', (string)$this->request->get['path']);
    } else {
      $parts = array();
    }
    if (isset($parts[0])) {
      $data['category_id'] = $parts[0]; //родительская
    } else {
      $data['category_id'] = 0;
    }
    if (isset($parts[1])) {
      $data['child_id'] = $parts[1]; //дочерняя
    } else {
      $data['child_id'] = 0;
    }

    if($data['child_id']) $data['category_id'] = null;

    $this->load->model('catalog/category');
    $this->load->model('catalog/product');
    $data['categories'] = array();
    $categories = $this->model_catalog_category->getCategories(0);

    //Загружаем модель которая работает с таблицей настроек модулей (oc_setting)
    $this->load->model('setting/setting');

    //Получаем значение - показывать ли изображения
    $category_images = $this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_images'];
    //Получаем значение - размеры изображений
    $category_images_height = $this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_img_height'];
    $category_images_weight = $this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_img_weight'];

    //Для дочерних категорий
    $category_children = $this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_children'];
    $category_child_images = $this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_child_images'];
    $category_child_images_height = $this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_child_img_height'];
    $category_child_images_weight = $this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_child_img_weight'];

    //Массив категорий, которые не нужно выводить
    if(isset($this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_checkbox'])){
      $categoryKsl_checkbox = $this->model_setting_setting->getSetting('categoryKsl')['categoryKsl_checkbox'];
    } else $categoryKsl_checkbox = null;

    foreach ($categories as $category) {

      //Пропускаем, если в настройках указано отключить данную категорию
      if (isset($categoryKsl_checkbox[$category['category_id']])) {
        continue;				
      }
      $children_data = array();

      if ($category_children) {

        $children = $this->model_catalog_category->getCategories($category['category_id']);

        foreach($children as $child) {
          //Пропускаем, если в настройках указано отключить данную категорию
          if (isset($categoryKsl_checkbox[$child['category_id']])) {
            continue;				
          }
      
          $filter_data = array('filter_category_id' => $child['category_id'], 'filter_sub_category' => true);

          //Работа с изображениями			
          if ($category_child_images && is_file(DIR_IMAGE . $child['image'])) {
            $this->load->model('tool/image');
            //метод создает изображения указанного размера
            $image = $this->model_tool_image->resize($child['image'], $category_child_images_weight, $category_child_images_height);
          } else {
            $image = '';
          }
      
          $children_data[] = array(
            'category_id' => $child['category_id'],
            'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
            'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
            'image' => $image,
          );
        }
      }
      
      $filter_data = array(
        'filter_category_id'  => $category['category_id'],
        'filter_sub_category' => true
      );

      //Работа с изображениями			
      if ($category_images && is_file(DIR_IMAGE . $category['image'])) {
        $this->load->model('tool/image');
        //метод создает изображения указанного размера
        $image = $this->model_tool_image->resize($category['image'], $category_images_weight, $category_images_height);
      } else {
        $image = '';
      }
      $data['is_home'] = !isset($this->request->get['route']) || ($this->request->get['route'] == 'common/home');
      $data['categories'][] = array(
        'category_id' => $category['category_id'],
        'name'        => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
        'children'    => $children_data,
        'href'        => $this->url->link('product/category', 'path=' . $category['category_id']),
        'image' => $image,
      );
    }	
    //Добавляем свои стили
    
    return $this->load->view('extension/module/categoryKsl', $data);
  }
}


Template (tpl):

<div class="list-group">
    <?php  ($categories as $category) { ?>
        <?php if ($category['category_id'] == $category_id) { ?>
            <div class="list-group-item active current">
                <div class="list-group-item-block">
                    <a href="" data-href="<?=$category['href']?>">
                        <img src="<?=$category['image']?>" class="img-responsive" />
                        <span class="img-cat-name cat-name"><?=$category['name']?></span>
                    </a>
                </div>
                <div class="list-group-item-child left">
                    <div class="list-group-arrow-back">
                        <i class="fa fa-long-arrow-left" aria-hidden="true"></i>
                        <span><?=$category['name']?></span>
                    </div>
                    <?php if ($category['children']) { ?>
                        <?php foreach ($category['children'] as $child) { ?>                        
                            <?php if ($child['category_id'] == $child_id) { ?>
                                <a href="<?=$child['href']?>" class="current">
                                    <img src="<?=$child['image']?>" class="img-responsive" />
                                    <span><?=$child['name']?></span>
                                </a>
                            <?php } else { ?>
                                <a href="<?=$child['href']?>">
                                    <img src="<?=$child['image']?>" class="img-responsive" />
                                    <span><?=$child['name']?></span>
                                </a>
                            <?php } ?>                               
                        <?php } ?>
                    <?php } else { ?>
                    <?php } ?>
                    <div class="categories-left-close-btn">
                        <div class="categories-left-close-btn-wrapper"></div>
                    </div>
                </div>
            </div>
        <?php } else { ?>
                <div class="list-group-item">
                    <div class="list-group-item-block">
                        <a href="" data-href="<?=$category['href'] ?>">
                            <img src="<?=$category['image']?>" class="img-responsive" />
                            <span class="img-cat-name cat-name"><?=$category['name']?></span>
                        </a>
                    </div>
                    <div class="list-group-item-child left">
                        <div class="list-group-arrow-back">
                            <i class="fa fa-long-arrow-left" aria-hidden="true"></i>
                            <a href="<?=$category['href'] ?>" class="parent-cat-name">
                                <span><?=$category['name']?></span>
                                <span class="parent-cat-name-description">Перейти в категорию</span>
                            </a>
                        </div>
                        <?php if ($category['children']) { ?>
                            <?php foreach ($category['children'] as $child) { ?>                        
                                <?php if ($child['category_id'] == $child_id) { ?>
                                    <a href="<?=$child['href']?>" class="current">
                                        <img src="<?=$child['image']?>" class="img-responsive" />
                                        <span><?=$child['name']?></span>
                                    </a>
                                <?php } else { ?>
                                    <a href="<?=$child['href']?>">
                                        <img src="<?=$child['image']?>" class="img-responsive" />
                                        <span><?=$child['name']?></span>
                                    </a>
                                <?php } ?>                               
                            <?php } ?>
                        <?php } else { ?>
                        <?php } ?>
                        <div class="categories-left-close-btn">
                            <div class="categories-left-close-btn-wrapper"></div>
                        </div>
                    </div>
                </div>
        <?php } ?>
    <?php }  ?>
</div>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question