drt12345678 2013-11-17 12:42
浏览 71
已采纳

如何在opencart admin / catalog / product_form.tpl上获取所有类别?

I'm using opencart 1.5.6 and what I'm trying to do is add a simple select with all the categories and children categories as option on product_form.tpl.

I tried add to the follow code at admin/controler/catalog/product.php:

    $this->load->model('catalog/category');

    $results = $this->model_catalog_category->getCategories(0);

    foreach ($results as $result) {

        $this->data['categories'][] = array(
            'category_id' => $result['category_id'],
            'name'        => $result['name']
        );
    }

and for the view admin/view/catalog/product_form.tpl:

<tr>
              <td>Categories</td>
              <td><select>
                      <option value="">All Categories</option>
                      <?php foreach ($categories as $category) { ?>
                      <option value="<?php echo $category['name']; ?>"><?php echo $category['name']; ?></option>
                      <?php } ?>
                  </select>
              </td>
          </tr>

but it's not working. It returns nothing.

I wish someone could help me and tell me what I'm missing.

  • 写回答

2条回答 默认 最新

  • dongpengyu1363 2013-11-18 09:41
    关注

    I think Your approach is good but one small mistake - the method getCategories() in administration part does not receive the $parent_id paramaters, but array $data parameter (for sorting and filtering possibilities). So if You want to load the categories in Your product controller, You can proceed like this (I am reusing Your code):

    $this->load->model('catalog/category');
    
    $this->data['categories'] = array();
    
    foreach ($this->model_catalog_category->getCategories(array()) as $category) {
        $this->data['categories'][] = array(
            'category_id' => $category['category_id'],
            'name'        => $category['name']
        );
    }
    

    And in Your template:

          <?php if($categories) { ?>
          <tr>
              <td>Categories</td>
              <td><select>
                      <option value="">All Categories</option>
                      <?php foreach ($categories as $category) { ?>
                      <option value="<?php echo $category['category_id']; ?>"><?php echo $category['name']; ?></option>
                      <?php } ?>
                  </select>
              </td>
          </tr>
          <?php } ?>
    

    I think this is almost the same as what You had, do not understand why it didn't work for You...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!