Opencart 3.x category id in product page

a 夏天 提交于 2020-01-24 01:57:06

问题


I'm using opencart 3.0.2.0

I'm trying to get category id in the product page.

Any suggestions ?


回答1:


If you just want to get category id in the product page

Open product.php file from catalog/controller/product

and search for

$product_info = $this->model_catalog_product->getProduct($product_id);

replace it with

$product_info = $this->model_catalog_product->getProduct($product_id);
        $query_categories = $this->model_catalog_product->getCategories($product_id);

        $categories = array();

        foreach ($query_categories as $cat) {
            $ocb_category = $this->model_catalog_category->getCategory($cat['category_id']);

            $category_info['category_id'] = $ocb_category['category_id'];
            $category_info['name'] = $ocb_category['name'];
            $data['categories'][] = $category_info; 
        }

open your product.twig file

paste this code in it

{% if (categories) %} 
    {% for category in categories %} 
        {% if category.category_id %} 
           <a>{{category.name}}:{{category.category_id}}<a><br>
        {% endif %} 
    {% endfor %} 
{% endif %}

Result: Category_name:Category_id (i.e. Electronics:223)

Note: If the product is in multiple categories it will print all the categories with their ids

Hope this might help you



来源:https://stackoverflow.com/questions/50213633/opencart-3-x-category-id-in-product-page

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