doujiacai4986 2013-05-07 13:57
浏览 60
已采纳

Opencart选择类别和子类别的下拉列表

I have a dropdown list containing categories and subcategories from my OpenCart shop, however I cannot seem to get the SEO keyword URL to show as the option value:

<select name="cat_id" id="category-select" class="span4">
    <option value="">Browse by category</option>
    <?php 
    $cats = $this->model_catalog_category->getCategories();
    foreach ($cats as $cat) {
        echo '<option value="' . $this->url->addRewrite('product/category', '&category_id=' . $cat['category_id']) . '">' . $cat['name'] . '</option>';
        $subcats = $this->model_catalog_category->getCategories($cat['category_id']);
        foreach ($subcats as $subcat) {
            echo '<option value="' . $this->url->addRewrite('product/category', '&category_id=' . $subcat['category_id'])  . '">- ' . $subcat['name'] . '</option>';
        }
    }
    ?>
</select>

I am new to OpenCart and not quite sure about what needs to go in the addRewrite function. Can't seem to find any mention of this in the OpenCart documentation either.

  • 写回答

1条回答 默认 最新

  • douhan8430 2013-05-07 14:33
    关注

    That's probably because of wrong method used. For SEO URL's You'd have to use the link() method. And of course You're forgetting about the MVC. The right approach would be to modify the controller which would load and prepare the data that will be passed to template then. Template is supposed to draw/present the data only...

    So let's assume it is the category controller and template, so let's edit the right controller first (catalog/controller/product/category.php):

    $this->data['my_categories'] = array();
    
    foreach($this->model_catalog_category->getCategories() as $category) {
        $this->data['my_categories'][] = array(
            'title' => $category['name'],
            'href'  => $this->url->link('product/category', 'path=' . $category['category_id']),
        );
    }
    

    This is only for example thus I do not load subcategories here, but the approach would be the same.

    Now present the data in the template (so catalog/view/theme/<YOUR_THEME>/template/product/category.tpl):

    <select name="cat_id" id="category-select" class="span4">
        <option value="">Browse by category</option>
        <?php if($my_categories) { ?>
        <?php foreach ($my_categories as $category) { ?>
        <option value="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></option>
        <?php } ?>
        <?php } ?>
    </select>
    

    EDIT: The SEO link for subcategory should be:

    'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $subcategory['category_id'])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛