Answer the question
In order to leave comments, you need to log in
How to link products to categories in a loop?
There is a controller in opencart: catalog/controller/extension/module/category.php
I get goods in the cycle there like this:
foreach ($categories as $category) {
$children_data = array();
if ($category['category_id'] == $data['category_id']) {
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach($children as $child) {
$filter_data = array('filter_category_id' => $child['category_id'], 'filter_sub_category' => true);
$children_data[] = array(
'category_id' => $child['category_id'],
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
}
$filter_data = array(
'filter_category_id' => $category['category_id'],
'filter_sub_category' => true
);
$data['categories'][] = array(
'category_id' => $category['category_id'],
'name' => $category['name'] . ( !isset( $data['is_home'] ) && $this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
'children' => $children_data,
'href' => $this->url->link('product/category', 'path=' . $category['category_id'])
);
$products_home = $this->model_catalog_product->getProducts( $filter_data['filter_category_id'] );
foreach ( $products_home as $product_home ) {
$data['products_home'][] = array(
'name' => $product_home['name'],
'href' => $this->url->link('product/product', 'product_id=' . $product_home['product_id'])
);
}
}
Answer the question
In order to leave comments, you need to log in
$products_home = $this->model_catalog_product->getProducts( $filter_data );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question