Все коды, которые вам нужны, уже существуют в файлах OpenCart. Например, вы можете перейти по ссылке: catalog/model/catalog/category.php
и создать копию функции getCategories
, переименовать и отредактировать ее.
Я создал сценарий vQmod, который выполняет нужную задачу, если вы не используете vQmod вы можете преобразовать его в ocmod или редактировать файлы вручную Вот скриншот результата:
и 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
Вау, это большая помощь! Ты мой герой! Однако я не так разбираюсь в opencart .... так что мне потребуется время, чтобы понять реализацию кода ... Мне также нужно преобразовать его в ocmod. Я не могу найти хорошую документацию по файлу ocmod. Единственное, что я нашел, это [link] (https://github.com/opencart/opencart/wiki/Modification-System) Например, что означает ошибка = «пропустить» (хотя в ocmod я не видел этот атрибут)? Или атрибут индекса? Вы знаете, где я могу найти больше информации об использовании ocmod? Но большое вам спасибо за помощь! –
Привет, пожалуйста, почему бы вам не использовать vQmod? Я скоро добавлю версию ocmod на свой пост. – DigitCart
У моего работодателя есть еще один разработчик, который рекомендует использовать ocmod ... Мне нечего сказать по этому поводу ... И еще раз спасибо!Хороших выходных! –