Post by prostage » Sat Dec 28, 2024 5:58 pm

Hello,
I'm new to Opencart 3. I like to modify common/menu.php
Instead of showing child category, I like to show the products that belong to the category .Thus, the main menu are the categories, and the pulldown shows the products. We do not use sub-categories. What exactly do I have to change in the code?
Many thanx, Lukas

Code: Select all

// Menu
		$this->load->model('catalog/category');

		$this->load->model('catalog/product');

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

		$categories = $this->model_catalog_category->getCategories(0);

		foreach ($categories as $category) {
			if ($category['top']) {
				// Level 2
				$children_data = array();

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

				// Level 1
				$data['categories'][] = array(
					'name'     => $category['name'],
					'children' => $children_data,
					'column'   => $category['column'] ? $category['column'] : 1,
					'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
				);
			}
		}
		
Last edited by prostage on Tue Dec 31, 2024 2:47 pm, edited 1 time in total.

Newbie

Posts

Joined
Thu Feb 13, 2014 3:49 am

Post by johnp » Sat Dec 28, 2024 7:12 pm


Opencart 1.5.6.5/OC Bootstrap Pro/VQMOD lover, user and geek.
Affordable Service £££ - Opencart Installs, Fixing, Development and Upgrades
Plus Ecommerce, Marketing, Mailing List Management and More
FREE Guidance and Advice at https://www.ecommerce-help.co.uk


User avatar
Active Member

Posts

Joined
Fri Mar 25, 2011 10:25 am
Location - Surrey, UK

Post by softmonke » Sun Dec 29, 2024 6:37 am

If you really want to do it yourself, you can just use the product model's ("catalog/model/catalog/product.php") "getProducts" function. Just pass an array containing "filter_category_id" as an argument. Something like the code below:

Code: Select all

$this->load->model('catalog/product');

$filter_data = [
    'filter_category_id' => 123
];
 
$products = $this->model_catalog_product->getProducts($filter_data);
Then you will also have to style the menu and decide how you are going to display the products.

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by prostage » Mon Dec 30, 2024 4:11 pm

softmonke wrote:
Sun Dec 29, 2024 6:37 am

Code: Select all

$this->load->model('catalog/product');

$filter_data = [
    'filter_category_id' => 123
];
 
$products = $this->model_catalog_product->getProducts($filter_data);
Thanx for this. I understand how to get the products, but I'm struggeling with the syntax I have to write inside the "foreach" loop.

Newbie

Posts

Joined
Thu Feb 13, 2014 3:49 am

Post by softmonke » Mon Dec 30, 2024 4:42 pm

prostage wrote:
Mon Dec 30, 2024 4:11 pm
Thanx for this. I understand how to get the products, but I'm struggeling with the syntax I have to write inside the "foreach" loop.
Perhaps you can be more specific as to what you are struggling with? Based on my code written above, you can just do a simply for-loop:

Code: Select all

// ...
$products = $this->model_catalog_product->getProducts($filter_data);

$data['dropdown_products'] = []; // you need to declare this so that your frontend can access the variable you've created

foreach ($products as $product) {
  $data['dropdown_products'][] = [
      'product_id' => $product['product_id'],
      'name'       => $product['name'],
      // ...
      // ...
  ];
}
// ...
and then at your template file, you loop "dropdown_products" as declared above in the "$data" array:

Code: Select all

{% for product in dropdown_products %}
<div>
  <p>{{ product.name }}</p>
</div>
{% endfor %}
That's just a very simple example, to incorporate it into your category code:

Code: Select all

// ...
$categories = $this->model_catalog_category->getCategories(0);

// make sure you load the model to prevent any errors
$this->load->model('catalog/product');

foreach ($categories as $category) {
  if ($category['top']) {
    // Level 2 - which is where you want to insert your products instead of the sub-categories
    $children_data = array();

    $filter_data = [
      'filter_category_id' => $category['category_id']
    ];

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

    foreach ($products as $product) {
      $children_data[] = [
        'name' => $product['name'],
        'href' => $this->url->link('product/product', 'product_id=' . $product['product_id'])
      ];
    }

    // Level 1
    $data['categories'][] = array(
      'name'     => $category['name'],
      'children' => $children_data,
      'column'   => $category['column'] ? $category['column'] : 1,
      'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
    );
  }
}
// ...
Please note that I didn't run a test on this code, so do test it on your own and make adjustments accordingly. This should automatically display your products with links to the products under your top-level category in your menu without having to make changes on your frontend.

If you need to style your menu or add additional product data, you will have to make more changes on your own, or like @johnp suggested, get a extension for more complicated usage, or hire a developer if you are not able to accomplish on your own.

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by prostage » Tue Dec 31, 2024 2:45 pm

Thanx for this more detailed sample. It works now!
And it is 10x easier than these complicated extension which is far over the top for what I need.

Newbie

Posts

Joined
Thu Feb 13, 2014 3:49 am
Who is online

Users browsing this forum: Semrush [Bot] and 7 guests