简体   繁体   English

Opencart 3.0 - 在类别页面上按 ID 显示类别中的所有产品

[英]Opencart 3.0 — Display All Products from Category by ID on Category Page

I switched over to Opencart 3.0.. Not to fond of the.tpl to.twig changes.我切换到 Opencart 3.0 .. 不喜欢 .tpl 到.twig 的变化。

Anyways, I have been trying, to no avail, to display all products from a particular category on another category page.无论如何,我一直在尝试在另一个类别页面上显示来自特定类别的所有产品,但无济于事。

I found this foreach loop:我发现了这个 foreach 循环:

{% for product in products %}

Which I imagine reads我想读

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

I tried adding the Loop to the path:我尝试将循环添加到路径:

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

but it only shows the products from the current category page id I am currently on.但它只显示我当前所在的当前类别页面 id 中的产品。

Any help would be greatly appreciated.任何帮助将不胜感激。

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.在 OC 上直接编辑文件通常是不好的做法,而是查看 OCMOD 或 VQMOD 进行运行时更改但不编辑核心文件。 Granted this may be an additional complication right now.当然,这现在可能是一个额外的并发症。

If you look at the category.php file in the /catalog/controller/product/ folder around line 150, you'll see these lines of code:如果您查看/catalog/controller/product/文件夹中第 150 行附近的 category.php 文件,您将看到以下代码行:

$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);

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.您需要做的是使用必要的过滤器创建一个新的$filter_data变量,如果这就是您所需要的,您可以只拥有类别 ID。

Look at the line below:看下面的行:

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

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). 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变量)。

The controller file we first looked at then iterates through these $results and stores the values in $data['products'] .我们首先查看的 controller 文件然后遍历这些$results并将值存储在$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.这不是一个完整的解决方案,因为 controller 文件会继续调整图像大小、获取评论等。因此您需要根据自己的需要进行调整并进行一些尝试。

Just make sure you're working on a local copy of the site!只需确保您正在处理该站点的本地副本!

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

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