2017-02-09 6 views
1

У меня есть несколько магазинов opencart2. Я пытаюсь создать меню, в котором отображаются все магазины со всеми их категориями и подкатегориями. Что-то вроде этогополучить список всех категорий и подкатегорий, если из магазина id в открытой корзине

store 1 

    --cat 1 

----subcat1 

----subcat2 

и так далее

Я знаю, что есть модуль, который называется Store, но он возвращает только список всех магазинов (shop name, shop id, shop url) не категории. Есть ли способ вызвать категории и подкатегории из идентификатора магазина или что-то в этом роде?

ответ

1

Все коды, которые вам нужны, уже существуют в файлах OpenCart. Например, вы можете перейти по ссылке: catalog/model/catalog/category.php и создать копию функции getCategories, переименовать и отредактировать ее.

Я создал сценарий vQmod, который выполняет нужную задачу, если вы не используете vQmod вы можете преобразовать его в ocmod или редактировать файлы вручную Вот скриншот результата: enter image description here

и XML-файл :

<?xml version="1.0" encoding="UTF-8"?> 
<modification> 
    <id>Stores List With Their Categories</id> 
    <version>2.x</version> 
    <vqmver>2.6.0</vqmver> 
    <author>[email protected]</author> 

    <file name="catalog/controller/module/store.php"> 
     <operation error="skip"> 
      <search position="replace" index="1"><![CDATA[$data['stores'][] = array(]]></search> 
      <add><![CDATA[ 
       $this->load->model('catalog/category'); 
       $this->load->model('catalog/product'); 

       $store_categories = array(); 
       $categories = $this->model_catalog_category->getStoreCategories(0); 

       foreach ($categories as $category) { 
        // Level 2 
        $children_data = array(); 

        $children = $this->model_catalog_category->getStoreCategories(0, $category['category_id']); 

        foreach ($children as $child) { 
         $filter_data = array(
          'filter_category_id' => $child['category_id'], 
          'filter_sub_category' => true 
         ); 

         $children_data[] = array(
          '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']) 
         ); 
        } 

        // Level 1 
        $store_categories[] = array(
         'name'  => $category['name'], 
         'children' => $children_data, 
         'column' => $category['column'] ? $category['column'] : 1, 
         'href'  => $this->url->link('product/category', 'path=' . $category['category_id']) 
        ); 
       } 

       $data['stores'][] = array(
        'categories' => $store_categories, 
      ]]></add> 
     </operation> 
     <operation error="skip"> 
      <search position="replace" index="2"><![CDATA[$data['stores'][] = array(]]></search> 
      <add><![CDATA[ 
       $store_categories = array(); 
       $categories = $this->model_catalog_category->getStoreCategories($result['store_id']); 

       foreach ($categories as $category) { 
         // Level 2 
         $children_data = array(); 

         $children = $this->model_catalog_category->getStoreCategories($result['store_id'], $category['category_id']); 

         foreach ($children as $child) { 
          $filter_data = array(
           'filter_category_id' => $child['category_id'], 
           'filter_sub_category' => true 
          ); 

          $children_data[] = array(
           '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']) 
          ); 
         } 

         // Level 1 
         $store_categories[] = array(
          'name'  => $category['name'], 
          'children' => $children_data, 
          'column' => $category['column'] ? $category['column'] : 1, 
          'href'  => $this->url->link('product/category', 'path=' . $category['category_id']) 
         ); 
       } 
       $data['stores'][] = array(
        'categories' => $store_categories, 
      ]]></add> 
     </operation> 
    </file> 

    <file name="catalog/view/theme/*/template/module/store.tpl"> 
     <operation error="skip"> 
      <search position="after"><![CDATA[<?php echo $store['name']; ?>]]></search> 
      <add><![CDATA[ 
       <?php if ($store['categories']) { ?> 
         <ul> 
         <?php foreach ($store['categories'] as $category) { ?> 
          <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a> 
          <?php if ($category['children']) { ?> 
            <?php foreach (array_chunk($category['children'], ceil(count($category['children'])/$category['column'])) as $children) { ?> 
            <ul> 
            <?php foreach ($children as $child) { ?> 
            <li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li> 
            <?php } ?> 
            </ul> 
            <?php } ?> 
          <?php } ?> 
          </li> 
         <?php } ?> 
         </ul> 
       <?php } ?> 
      ]]></add> 
     </operation> 
    </file> 

    <file name="catalog/model/catalog/category.php"> 
     <operation error="skip"> 
      <search position="before"><![CDATA[public function getCategories($parent_id = 0) {]]></search> 
      <add><![CDATA[ 
       public function getStoreCategories($store_id, $parent_id = 0) { 
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$store_id . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)"); 

        return $query->rows; 
       } 
      ]]></add> 
     </operation> 
    </file> 

</modification> 

Edit: Вот ocmod версия, протестирован на OpenCart 2.0.3.1 с темой по умолчанию.

Для файла OCMOD быть загружено расширение файла должно быть .ocmod.zip или .ocmod.xml. Это делается для того, чтобы не удалять файлы OCMOD , загруженные в администратор магазина пользователей.

<?xml version="1.0" encoding="UTF-8"?> 
<modification> 
    <name>Stores List With Their Categories</name> 
    <version>2.x</version> 
    <author>[email protected]</author> 
    <code>Stores List With Their Categories</code> 

    <file path="catalog/controller/module/store.php"> 
     <operation> 
      <search index="0"><![CDATA[$data['stores'][] = array(]]></search> 
      <add position="replace"><![CDATA[ 
       $this->load->model('catalog/category'); 
       $this->load->model('catalog/product'); 

       $store_categories = array(); 
       $categories = $this->model_catalog_category->getStoreCategories(0); 

       foreach ($categories as $category) { 
        // Level 2 
        $children_data = array(); 

        $children = $this->model_catalog_category->getStoreCategories(0, $category['category_id']); 

        foreach ($children as $child) { 
         $filter_data = array(
          'filter_category_id' => $child['category_id'], 
          'filter_sub_category' => true 
         ); 

         $children_data[] = array(
          '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']) 
         ); 
        } 

        // Level 1 
        $store_categories[] = array(
         'name'  => $category['name'], 
         'children' => $children_data, 
         'column' => $category['column'] ? $category['column'] : 1, 
         'href'  => $this->url->link('product/category', 'path=' . $category['category_id']) 
        ); 
       } 

       $data['stores'][] = array(
        'categories' => $store_categories, 
      ]]></add> 
     </operation> 
     <operation> 
      <search index="1"><![CDATA[$data['stores'][] = array(]]></search> 
      <add position="replace"><![CDATA[ 
       $store_categories = array(); 
       $categories = $this->model_catalog_category->getStoreCategories($result['store_id']); 

       foreach ($categories as $category) { 
         // Level 2 
         $children_data = array(); 

         $children = $this->model_catalog_category->getStoreCategories($result['store_id'], $category['category_id']); 

         foreach ($children as $child) { 
          $filter_data = array(
           'filter_category_id' => $child['category_id'], 
           'filter_sub_category' => true 
          ); 

          $children_data[] = array(
           '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']) 
          ); 
         } 

         // Level 1 
         $store_categories[] = array(
          'name'  => $category['name'], 
          'children' => $children_data, 
          'column' => $category['column'] ? $category['column'] : 1, 
          'href'  => $this->url->link('product/category', 'path=' . $category['category_id']) 
         ); 
       } 
       $data['stores'][] = array(
        'categories' => $store_categories, 
      ]]></add> 
     </operation> 
    </file> 

    <file path="catalog/view/theme/*/template/module/store.tpl"> 
     <operation> 
      <search><![CDATA[<?php echo $store['name']; ?>]]></search> 
      <add position="after"><![CDATA[ 
       <?php if ($store['categories']) { ?> 
         <ul> 
         <?php foreach ($store['categories'] as $category) { ?> 
          <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a> 
          <?php if ($category['children']) { ?> 
            <?php foreach (array_chunk($category['children'], ceil(count($category['children'])/$category['column'])) as $children) { ?> 
            <ul> 
            <?php foreach ($children as $child) { ?> 
            <li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li> 
            <?php } ?> 
            </ul> 
            <?php } ?> 
          <?php } ?> 
          </li> 
         <?php } ?> 
         </ul> 
       <?php } ?> 
      ]]></add> 
     </operation> 
    </file> 

    <file path="catalog/model/catalog/category.php"> 
     <operation> 
      <search><![CDATA[public function getCategories($parent_id = 0) {]]></search> 
      <add position="before"><![CDATA[ 
       public function getStoreCategories($store_id, $parent_id = 0) { 
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$store_id . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)"); 

        return $query->rows; 
       } 
      ]]></add> 
     </operation> 
    </file> 

</modification> 

Смотрите эту хорошую статью о vqmod и ocmod различия: https://forum.opencart.com/viewtopic.php?f=24&t=131995

+0

Вау, это большая помощь! Ты мой герой! Однако я не так разбираюсь в opencart .... так что мне потребуется время, чтобы понять реализацию кода ... Мне также нужно преобразовать его в ocmod. Я не могу найти хорошую документацию по файлу ocmod. Единственное, что я нашел, это [link] (https://github.com/opencart/opencart/wiki/Modification-System) Например, что означает ошибка = «пропустить» (хотя в ocmod я не видел этот атрибут)? Или атрибут индекса? Вы знаете, где я могу найти больше информации об использовании ocmod? Но большое вам спасибо за помощь! –

+0

Привет, пожалуйста, почему бы вам не использовать vQmod? Я скоро добавлю версию ocmod на свой пост. – DigitCart

+0

У моего работодателя есть еще один разработчик, который рекомендует использовать ocmod ... Мне нечего сказать по этому поводу ... И еще раз спасибо!Хороших выходных! –

0

Я преобразовал файл XML из @Mojtaba Sabeti в ocmod файл.

Я сомневаюсь в атрибуте индекса. Я где-то читал, что индекс ocmod начинается с «0». поэтому первое вхождение будет index = "0". Это правильно?

Мне также нужно загрузить выходные данные модуля Stores в файл шаблона меню. Поэтому я добавил пару строк в нижней части модификации, чтобы сделать это. В основном я пытаюсь загрузить контроллер модуля «Магазины» в контроллер меню. Я делаю это прямо перед загрузкой файла шаблона. Контроллер меню находится в каталоге/controller/journal2/menu.php. Правильно ли это?

<?xml version="1.0" encoding="UTF-8"?> 
<modification> 
<id>Stores List With Their Categories</id> 
<version>2.x</version> 
<vqmver>2.6.0</vqmver> 
<author>[email protected]</author> 

<file path="catalog/controller/module/store.php"> 
    <operation> 
     <search><![CDATA[$data['stores'][] = array(]]></search> 
     <add position="replace" index="1"><![CDATA[ 
      $this->load->model('catalog/category'); 
      $this->load->model('catalog/product'); 

      $store_categories = array(); 
      $categories = $this->model_catalog_category->getStoreCategories(0); 

      foreach ($categories as $category) { 
       // Level 2 
       $children_data = array(); 

       $children = $this->model_catalog_category->getStoreCategories(0, $category['category_id']); 

       foreach ($children as $child) { 
        $filter_data = array(
         'filter_category_id' => $child['category_id'], 
         'filter_sub_category' => true 
        ); 

        $children_data[] = array(
         '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']) 
        ); 
       } 

       // Level 1 
       $store_categories[] = array(
        'name'  => $category['name'], 
        'children' => $children_data, 
        'column' => $category['column'] ? $category['column'] : 1, 
        'href'  => $this->url->link('product/category', 'path=' . $category['category_id']) 
       ); 
      } 

      $data['stores'][] = array(
       'categories' => $store_categories, 
     ]]></add> 
    </operation> 
    <operation> 
     <search><![CDATA[$data['stores'][] = array(]]></search> 
     <add position="replace" index="2"><![CDATA[ 
      $store_categories = array(); 
      $categories = $this->model_catalog_category->getStoreCategories($result['store_id']); 

      foreach ($categories as $category) { 
        // Level 2 
        $children_data = array(); 

        $children = $this->model_catalog_category->getStoreCategories(0, $category['category_id']); 

        foreach ($children as $child) { 
         $filter_data = array(
          'filter_category_id' => $child['category_id'], 
          'filter_sub_category' => true 
         ); 

         $children_data[] = array(
          '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']) 
         ); 
        } 

        // Level 1 
        $store_categories[] = array(
         'name'  => $category['name'], 
         'children' => $children_data, 
         'column' => $category['column'] ? $category['column'] : 1, 
         'href'  => $this->url->link('product/category', 'path=' . $category['category_id']) 
        ); 
      } 
      $data['stores'][] = array(
       'categories' => $store_categories, 
     ]]></add> 
    </operation> 
</file> 

<file path="catalog/view/theme/*/template/module/store.tpl"> 
    <operation error="skip"> 
     <search position="after"><![CDATA[<?php echo $store['name']; ?>]]></search> 
     <add><![CDATA[ 
      <?php if ($store['categories']) { ?> 
        <ul> 
        <?php foreach ($store['categories'] as $category) { ?> 
         <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a> 
         <?php if ($category['children']) { ?> 
           <?php foreach (array_chunk($category['children'], ceil(count($category['children'])/$category['column'])) as $children) { ?> 
           <ul> 
           <?php foreach ($children as $child) { ?> 
           <li><a href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?></a></li> 
           <?php } ?> 
           </ul> 
           <?php } ?> 
         <?php } ?> 
         </li> 
        <?php } ?> 
        </ul> 
      <?php } ?> 
     ]]></add> 
    </operation> 
</file> 

<file path="catalog/model/catalog/category.php"> 
    <operation> 
     <search><![CDATA[public function getCategories($parent_id = 0) {]]></search> 
     <add position="before"><![CDATA[ 
      public function getStoreCategories($store_id, $parent_id = 0) { 
       $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$store_id . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)"); 

       return $query->rows; 
      } 
     ]]></add> 
    </operation> 
</file> 

<file path="catalog/controller/journal2/menu.php"> 
    <operation> 
    <search><![CDATA[$this->template = $this->config->get('config_template') . '/template/journal2/menu/main.tpl';]]></search> 
    <add position="before"><![CDATA[ 
    $data['ac_all_stores'] = $this->load->controller('module/store'); 
    ]]></add> 
    </operation> 
</file> 

+0

Привет, я добавил версию ocmod в свой ответ. испытайте его и дайте мне знать, если у вас возникнут проблемы. для отображения списка магазинов в верхнем меню, пожалуйста, откройте новый поток. – DigitCart