我有一個多個opencart2
店。我嘗試創建一個菜單,其中顯示所有類別和子類別的商店。事情是這樣的獲得所有類別和子類別的列表,如果從開店車的店鋪ID
store 1
--cat 1
----subcat1
----subcat2
等
我知道有一個叫Store
模塊,但它僅返回的所有店鋪(shop name, shop id, shop url)
不是分類列表。有沒有辦法從商店ID或類似的東西中調用類別和子類別?
我有一個多個opencart2
店。我嘗試創建一個菜單,其中顯示所有類別和子類別的商店。事情是這樣的獲得所有類別和子類別的列表,如果從開店車的店鋪ID
store 1
--cat 1
----subcat1
----subcat2
等
我知道有一個叫Store
模塊,但它僅返回的所有店鋪(shop name, shop id, shop url)
不是分類列表。有沒有辦法從商店ID或類似的東西中調用類別和子類別?
您需要的所有代碼已經存在於OpenCart文件中。 例如,您可以訪問:catalog/model/catalog/category.php
並創建一個getCategories
函數的副本,重命名並編輯它。
我創建了執行所需的任務,如果你不使用vQmod你可以將它轉換爲ocmod或編輯文件手動下面是結果的一個截圖vQmod腳本:
和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>
編輯: 這裏是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
我已經轉換@Mojtaba Sabeti的xml文件ocmod文件。
我懷疑索引屬性。我讀過的地方在ocmod索引從'0'開始。所以第一次出現將是index =「0」。是對的嗎?
我還需要將商店模塊的輸出加載到菜單模板文件。所以我在修改的底部添加了幾行代碼來做到這一點。我基本上試圖將商店模塊的控制器加載到菜單的控制器中。我在模板文件的加載之前執行它。菜單的控制器位於目錄/ 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>
嗨,我已經添加ocmod版本到我的答案。測試它,讓我知道你是否有任何問題。要顯示頂級菜單中的商店列表,請打開一個新線程。 – DigitCart
哇,這是一個很大的幫助!你是我的英雄!然而,我不是那種經驗豐富的opencart ....所以它需要我來理解代碼的實現......我也需要將它轉換爲ocmod。我無法找到關於ocmod文件的很好的文檔。我發現的唯一的事情是[鏈接](https://github.com/opencart/opencart/wiki/Modification-System)例如error =「skip」是什麼意思(儘管在ocmod中,我沒有看到該屬性)?還是索引屬性?你知道我在哪裏可以找到關於使用ocmod的更多信息嗎?但非常感謝你的幫助! –
嗨,不客氣,你爲什麼不使用vQmod?我將很快將ocmod版本添加到我的帖子中。 – DigitCart
我的僱主有另一位開發人員推薦使用ocmod ...我無話可說...再次感謝!週末愉快! –