简体   繁体   English

在产品页面上获取类别父ID-opencart

[英]get category parent id on product page - opencart

Is there a way to get the category parent_id of a product, in the product page of Opencart v1.5.4.1. 在Opencart v1.5.4.1的产品页面中,是否可以获取产品类别的parent_id。 What am trying to change the button href based on the category parent_id.. If parent_id = 20 then the button should have href1 else href2. 试图根据类别parent_id更改按钮href的内容。如果parent_id = 20,则按钮应具有href1,否则href2。

So far, i've done this but its not working. 到目前为止,我已经做到了,但是没有用。

Added before " $this->load->model('tool/image'); " 在“ $this->load->model('tool/image'); ”之前添加

$product_cat = $this->model_catalog_product->getCategories($product_id);      
$product_cat_parent = $this->model_catalog_category->getCategory($product_cat[0]['category_id']);
            if ($product_cat_parent['parent_id'] == '20') {
                $this->data['sizeguide'] = $this->url->link('faq/faq/info', 'fpath=12');
            } else {
                $this->data['sizeguide'] = $this->url->link('faq/faq/info', 'fpath=13');
            }

in template file: 在模板文件中:

<a class="button2 sizeguidebox" href="<?php echo $sizeguide; ?>"><?php echo $text_sizeguide; ?></a>

This looks more complicated but it actually simpler and more efficient as a fool proof method of checking. 这看起来更复杂,但实际上,它是一种简单的检查方法,更加有效。 It will pick all of the categories related to the current product, find all their category info, and filter the categories with the parent_id of 20. If the query has any results (ie one of the categories is a subcategory of the category with ID 20) then it will set the faq info accordingly 它将选择与当前产品相关的所有类别,找到其所有类别信息,并使用parent_id为20过滤类别。如果查询有任何结果(即,其中一个类别是ID为20的类别的子类别) ),那么它将相应地设置常见问题信息

$product_cat = $this->model_catalog_product->getCategories($product_id);
$result = $this->db->query("
SELECT
    `c`.`parent_id`
FROM
    `" . DB_PREFIX . "category` `c`
LEFT JOIN
    `" . DB_PREFIX . "product_to_category` `p2c`
ON
    `c`.`category_id` = `p2c`.`category_id`
WHERE
    `c`.`parent_id` = '20'
AND
    `p2c`.`product_id` = '" . (int) $product_id . "'
");

if($result->num_rows > 0) {
    $this->data['sizeguide'] = $this->url->link('faq/faq/info', 'fpath=12');
} else {
    $this->data['sizeguide'] = $this->url->link('faq/faq/info', 'fpath=13');
}

Note that this hasn't been tested but should work in theory 请注意,这尚未经过测试,但理论上应该可以工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM