Opencart 3.0-在类别页面上按ID显示类别中的所有产品 [英] Opencart 3.0 -- Display All Products from Category by ID on Category Page

查看:356
本文介绍了Opencart 3.0-在类别页面上按ID显示类别中的所有产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我切换到了Opencart 3.0..不喜欢将.t​​pl更改为.twig.

I switched over to Opencart 3.0.. Not to fond of the .tpl to .twig changes.

无论如何,我一直都试图在另一个类别页面上显示特定类别的所有产品,但无济于事.

Anyways, I have been trying, to no avail, to display all products from a particular category on another category page.

我发现了这个foreach循环:

I found this foreach loop:

{% for product in products %}

我想读的是

<?php foreach ($products as $product) { //do something } ?>

我尝试将Loop添加到路径:

I tried adding the Loop to the path:

catalog/view/theme/*/template/product/category.twig

,但它仅显示我当前使用的当前类别页面ID中的产品.

but it only shows the products from the current category page id I am currently on.

任何帮助将不胜感激.

推荐答案

在OC上通常不建议直接编辑文件,而是查看OCMOD或VQMOD进行运行时更改,但不编辑核心文件,这是一种不好的做法.现在,这可能会带来额外的复杂性.

It's generally bad practise on OC to edit the files directly, rather look at OCMOD or VQMOD to make runtime changes but not edit the core file. Granted this may be an additional complication right now.

如果您查看第150行附近/catalog/controller/product/文件夹中的category.php文件,则会看到以下代码行:

If you look at the category.php file in the /catalog/controller/product/ folder around line 150, you'll see these lines of code:

$data['products'] = array();

$filter_data = array(
    'filter_category_id' => $category_id,
    'filter_filter'      => $filter,
    'sort'               => $sort,
    'order'              => $order,
    'start'              => ($page - 1) * $limit,
    'limit'              => $limit
);

$product_total = $this->model_catalog_product->getTotalProducts($filter_data);

$results = $this->model_catalog_product->getProducts($filter_data);

您需要做的是使用必要的过滤器创建一个新的$filter_data变量,如果仅此而已,您可以拥有类别ID.

What you need to do is create a new $filter_data variable with your requisite filters, you can just have the category ID if that's all you need.

查看下面的行:

$results = $this->model_catalog_product->getProducts($filter_data);

它调用位于模型CatalogCategory(/catalog/model/product.php)中的方法getProducts,该方法将基于传递给该方法的过滤器构建一个SQL查询,并返回与结果相关联的数组(因此命名为$results变量).

It's calling the method getProducts which is located in the model CatalogCategory (/catalog/model/product.php) this method will build a SQL query based on the filters passed to the method and return an associative array with the results (hence the aptly named $results variable).

我们首先查看的控制器文件,然后遍历这些$results,并将值存储在$data['products']中.

The controller file we first looked at then iterates through these $results and stores the values in $data['products'].

将其放在一起,您可以尝试以下操作:

Putting this together, you can try the following:

$data['products2'] = array();

$new_filter_data = array(
    'filter_category_id' => 13 // or any relevant ID
);

$results2 = $this->model_catalog_product->getProducts($new_filter_data);

这不是一个完整的解决方案,因为控制器文件会继续调整图像大小,获取评论等.因此,您需要根据需要进行调整并稍作调整.

This isn't a complete solution as the controller file continues to resize images, get reviews etc. So you'll need to adjust this according to what your needs are and play around a little.

只需确保您正在使用网站的本地副本即可!

Just make sure you're working on a local copy of the site!

这篇关于Opencart 3.0-在类别页面上按ID显示类别中的所有产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆