G
G
Genri_Rus2020-12-18 22:26:02
PHP
Genri_Rus, 2020-12-18 22:26:02

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'])
    );
  }
}


But this is how all products are displayed:
5fdd0bc6bb6d0043168975.png
How can I connect them in an iteration loop and add them to an array, for example, as in this example?
5c1ny4K.png

The fact is that there is no cat_id or category_id key in the product array

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nikolaev, 2020-12-19
@Genri_Rus

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question