如果来自打开购物车中的商店 ID,则获取所有类别和子类别的列表

get list of all categories and subcategories if from shop id in open cart

我有多家 opencart2 商店。我尝试创建一个菜单,其中显示所有商店及其所有类别和子类别。像这样

store 1

  --cat 1

----subcat1

----subcat2

等等

我知道有一个名为 Store 的模块,但它 returns 只是所有商店的列表 (shop name, shop id, shop url) 而不是类别。有没有办法从商店 ID 或类似的东西调用类别和子类别?

您需要的所有代码都已存在于 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>sabeti05@gmail.com</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 上测试。

For a OCMOD file to be uploaded the file extension must be either .ocmod.zip or .ocmod.xml. This is to avoid none OCMOD files from being uploaded to a store users admin.

<?xml version="1.0" encoding="UTF-8"?>
<modification>
    <name>Stores List With Their Categories</name>
    <version>2.x</version>
    <author>sabeti05@gmail.com</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"。是吗?

我还需要将商店模块的输出加载到菜单模板文件中。所以我在修改的底部添加了几行来这样做。我基本上尝试将 Stores 模块的控制器加载到 Menu 的控制器。我在加载模板文件之前就这样做了。菜单控制器位于 catalog/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>sabeti05@gmail.com</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>