get category products by category id in opencart

佐手、 提交于 2019-12-11 19:25:22

问题


hi i want to customize the feature module that as category is set through admin display the relevant product instead of featured module

when ever i select the category it displays category products what will be the code of controller which get category n give products array to that category

below is the view code of category select input

        <td><select name="category_id">

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

        </select>

回答1:


$categories = $this->model_catalog_product->getProductCategories(
    $this->request->get['product_id']);
foreach($categories as $category_id) {
   // do something
}

Add the getProductCategories function into model/catalog/product.php

public function getProductCategories($product_id) {
    $product_category_data = array();
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX .
           "product_to_category WHERE product_id = '" . (int)$product_id . "'");
    foreach ($query->rows as $result) {
            $product_category_data[] = $result['category_id'];
    }
    return $product_category_data;
}


来源:https://stackoverflow.com/questions/18967116/get-category-products-by-category-id-in-opencart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!