Opencart - 只加载一些类别?

时间:2014-08-18 12:55:04

标签: opencart categories

任何人都可以帮助我吗? 有模块。我只需要加载10个类别(例如ID 2,5,10,14等...)。 这是代码:

        //categories
        $this->load->model("catalog/category");
        $allCategories = $this->model_catalog_category->getCategories(array());
        $categoria_shop = array();

        foreach ($allCategories as $key => $value)
        {
            $categoria_shop[$value['name']] = $value['category_id'];
        }

        $categoria_shop_nezaradene = 69;

如何添加类别ID

2 个答案:

答案 0 :(得分:0)

catalog/model/catalog/category.php文件中,您可以看到函数getCategories()具有可选的parent_id参数,且必须为int,因此请尝试以下操作:

// Not tested

$category_ids = array(2, 5, 10, 14);
$categoria_shop = array(array());

foreach($category_ids as $category_id) {
    $categories = $this->model_catalog_category->getCategories($category_id);
    foreach ($categories as $key => $value) {
        $categoria_shop[][$value['name']] = $value['category_id'];
    }
}

输出示例:

$categoria_shop[0]['cat_name2'] = 1;
$categoria_shop[1]['cat_name5'] = 2;
$categoria_shop[2]['cat_name10'] = 3;
$categoria_shop[3]['cat_name14'] = 4;

答案 1 :(得分:0)

如何仅加载某些类别的最佳选择是向类别模型添加新方法(catalog/model/catalog/category.php),让我们将其称为getMultipleCategories。它可能看起来像这样:

public function getMultipleCategories($category_ids = array()) {
    if (empty($category_ids) {
        return array();
    }

    $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.category_id IN (" . implode(", ", $category_ids) . ")
            AND cd.language_id = " . (int)$this->config->get('config_language_id') . "
            AND c2s.store_id = " . (int)$this->config->get('config_store_id') . "
            AND c.status = 1
        ORDER BY c.sort_order, LCASE(cd.name)
    ");

    return $query->rows;
}

此方法(以及它的查询)加载所有带有在数组参数中传递的ID的类别(如果存在)。

现在在您的控制器中,您可以像这样调用此方法:

$categories = $this->model_catalog_category->getMultipleCategories(array(2, 4, 10, 14, 25));

如果您可以将此修改(模型)封装到vQmod扩展中,以便在升级OC版本时该方法不会消失,那会更好。