简体   繁体   English

OpenCart 2-在产品页面上显示类别标题

[英]OpenCart 2 - Show Category Title on Product Page

I searched and found results, modules and solution but all are for open cart 1, i want for open cart 2. 我搜索并找到了结果,模块和解决方案,但所有这些都用于打开购物车1,我想要打开购物车2。

in product.tpl 在product.tpl中

  <ul class="list-unstyled">
    <?php if ($manufacturer) { ?>
    <li><?php echo $text_manufacturer; ?> <a href="<?php echo $manufacturers; ?>"><?php echo $manufacturer; ?></a></li>
    <?php } ?>
    <li><?php echo $text_model; ?> <?php echo $model; ?></li>
    <?php if ($reward) { ?>
    <li><?php echo $text_reward; ?> <?php echo $reward; ?></li>
    <?php } ?>
    <li><?php echo $text_stock; ?> <?php echo $stock; ?></li>
  </ul>

i want to show category title in this list. 我想在此列表中显示类别标题。 i could not find this option in admin panel then i decided to do it manually. 我在管理面板中找不到此选项,然后我决定手动进行。 i know must do something in product.php but no idea how to get product name 我知道必须在product.php中做些什么,但不知道如何获取产品名称

$data['category_title'] = $this->request->get['category_title'];

thanks in advance 提前致谢

Pedram, Your website URL should be Pedram,您的网站网址应为

index.php?route=product/product&path=20_27&product_id=41

Here the path=20 → category_id 这里的路径= 20→category_id

But your URL have not path variable because you was directly go throgh on search page or home page product listing. 但是您的网址没有路径变量,因为您直接进入了搜索页或首页产品列表。 If you want to get category title then you want to develop code through product_id. 如果要获取类别标题,则要通过product_id开发代码。

Here is code to Getting Category Title 这是获取类别标题的代码

product.php → controller file product.php→控制器文件

$Categories = $this->model_catalog_category->getCategoryName($this->request->get['product_id']);

category.php → Model file category.php→模型文件

public function getCategoryName($product_id) {
       $query = $this->db->query("SELECT DISTINCT(cd.name),p2c.product_id,cd.category_id FROM " . DB_PREFIX . "product_to_category p2c, " . DB_PREFIX . "category_description cd WHERE p2c.product_id = '".$product_id."' AND cd.category_id = p2c.category_id AND cd.category_id != 1");    
       return $query->rows;
}

If your product select under more then one category then it will give array of result otherwise it will give one result. 如果您的产品在多个类别下选择,那么它将给出一组结果,否则将给出一个结果。

Pedram, If you do not want to select product into multiple category then change following code. Pedram,如果您不想将产品选择到多个类别中,请更改以下代码。

product.php(Controller) product.php(控制器)

$data['category_title'] = $this->model_catalog_category->getCategoryName($this->request->get['product_id']);

category.php(Model) category.php(模型)

public function getCategoryName($product_id) { 
$query = $this-db-query("SELECT DISTINCT(cd.name),p2c.product_id,cd.category_id FROM " . DB_PREFIX . "product_to_category p2c, " . DB_PREFIX . "category_description cd WHERE p2c.product_id = '".$product_id."' AND cd.category_id = p2c.category_id AND cd.category_id != 1");     

return $query-row;}  

product.tpl (View) product.tpl(查看)

<?php echo $category_title;?>

I know late to answer but i might help others . 我知道答案晚了,但我可能会帮助别人。 Tested in Opencart 2.3.0.2 在Opencart 2.3.0.2中测试

To Show category in Product page. 在产品页面上显示类别。

You have to edit some of the opencart files. 您必须编辑一些opencart文件。 Go to Controllors > product > product.php 转到Controllors>产品> product.php

Find the following line 找到以下行

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

As we need the category for the product list we need the product_id for each products in the list 因为我们需要产品列表的类别,所以我们需要列表中每个产品的product_id

$this->load->model('catalog/category');
$categories = $this->model_catalog_category->getCategoryName($this->request->get['product_id']); if ($categories)
                                    {
                                                           $categories_info= $this->model_catalog_category->getCategory($categories[0]['category_id']);
                                                            $data['category_title']  = $categories_info['name']; 
                                                            $data['category_text'] = "Category : ";
                                                            //print_r($categories_info['name']);
                                                           // $this->data['category_title'] = $categories_info['name'];
                                    }

Now Go to Model > Catalog > Category and you need to create a new function getCategoryName that we used in controller 现在转到“模型”>“目录”>“类别”,您需要创建一个新函数getCategoryName,我们在控制器中使用了该函数

  public function getCategoryName($product_id) {
                                  $query = $this->db->query("SELECT DISTINCT(cd.name),p2c.product_id,cd.category_id FROM " . DB_PREFIX . "product_to_category p2c, " . DB_PREFIX . "category_description cd WHERE p2c.product_id = '".$product_id."' AND cd.category_id = p2c.category_id AND cd.category_id != 1");    
                                                    return $query->rows;
                    }

Now go to View > theme > themename > template > product > product.tpl and add following line where you want to show category 现在转到查看>主题>主题名称>模板>产品> product.tpl,并在要显示类别的位置添加以下行

<?php echo $category_text; ?><?php echo $category_title; ?>

I do something like this. 我做这样的事情。 If you do not want to select product into multiple categories then change the following code. 如果您不想将产品选择为多个类别,请更改以下代码。

(catalog/controller/product/product.php) - path to controller file (system/storage/modification/catalog/controller/product/product.php) - path to opencart controller file (catalog / controller / product / product.php)-控制器文件的路径(system / storage / modification / catalog / controller / product / product.php)-opencart控制器文件的路径

Past this code on two file our controller. 将此代码粘贴到我们的控制器的两个文件上。 I past before this line: $product_info = $this->model_catalog_product->getProduct($product_id); 我在此行之前经过: $product_info = $this->model_catalog_product->getProduct($product_id);

Because we will already have our product id in variable: 因为我们已经在变量中包含了我们的产品ID:

$category_information = $this->model_catalog_category->getCategoryName($product_id);

if ($category_information) {
    $data['category_name'] = $category_information['name'];
}

(catalog/model/catalog/category.php) - path to model file (catalog / model / catalog / category.php)-模型文件的路径

Past this code before last close sign "}". 在最后一个关闭符号“}”之前经过此代码。 You create new function in our class of model: 您可以在我们的模型类中创建新函数:

public function getCategoryName($product_id) { 
$query = $this->db->query("SELECT DISTINCT(cd.name),p2c.product_id,cd.category_id FROM " . DB_PREFIX . "product_to_category p2c, " . DB_PREFIX . "category_description cd WHERE p2c.product_id = '".$product_id."' AND cd.category_id = p2c.category_id AND cd.category_id != 1");     
return $query->row;
}  

(catalog/view/theme/nameyoutheme/template/product/product.tpl) And paste this code wherever you need on product tpl. (目录/视图/主题/名称主题/模板/product/product.tpl),然后将此代码粘贴到产品tpl上的任何位置。 I past after <ul class="breadcrumb">...</ul> : 我过去<ul class="breadcrumb">...</ul>

<?php if(isset($category_name)){ ?>
    <div class="heading-title">
        <h1><?=$category_name;?></h1>
    </div>
<?php } ?>

And that's all folks. 那就是所有人。

PS OpenCart Version 2.3.0.2 PS OpenCart版本2.3.0.2

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

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