Page 1 of 1
[SOLVED]Showing Products in Main Menu
Posted: Thu Jul 27, 2017 3:19 am
by eddy-r3
Hey guys, trying to get products into the main menu, under their parent category, how would i go around this?
I've tried a few methods, but all seem to kick out an error, as they seem to be for previous versions of OC. I understand i need to edit the header controller, but how/what/where i have no idea!
Help please?
Re: Showing Products in Main Menu
Posted: Thu Jul 27, 2017 5:23 am
by eddy-r3
Anyone anyone?
Re: Showing Products in Main Menu
Posted: Thu Jul 27, 2017 6:31 am
by IP_CAM
probably not, at least as long as you refuse to inform about OC Version and Theme used...
Ernie
Re: Showing Products in Main Menu
Posted: Thu Jul 27, 2017 6:36 am
by eddy-r3
Oops apologies!
version 2.3.0.2
Theme - Custom making but based around default theme...
Re: Showing Products in Main Menu
Posted: Thu Jul 27, 2017 9:38 am
by IP_CAM
Re: Showing Products in Main Menu
Posted: Thu Jul 27, 2017 3:31 pm
by eddy-r3
Thanks for the reply, yes similar! I only need the href and the product name though
Re: Showing Products in Main Menu
Posted: Fri Jul 28, 2017 1:48 am
by eddy-r3
Hey guys, sorry to bang on about it, but can someone help me with the header.php controller in getting the product href and name into the header.tpl file?
Many thanks!
Re: Showing Products in Main Menu
Posted: Sat Jul 29, 2017 12:10 am
by eddy-r3
No one knows how to edit the controller to get product info? Or does no one want to help?
Im unsure of which it is....
Re: Showing Products in Main Menu
Posted: Sat Jul 29, 2017 10:07 am
by IP_CAM
Well, if you're just looking for the Code, why not check for it in some free extensions ?!
It all exists, so, where is the problem ?
Ernie
---
Images menu category v. 2.0.0.0, 2.0.1.0, 2.0.1.1, 2.0.2.0, 2.0.3.1
https://www.opencart.com/index.php?rout ... n_id=24065
---
Category Menu Image Display v.1.5.x, 2.0.0.0
https://www.opencart.com/index.php?rout ... n_id=28275
---
Flexible Menu v.2.0.3.1, 2.1.0.1, 2.1.0.2
https://www.opencart.com/index.php?rout ... n_id=22754
---
Menu Category Image v.1.5.6.x
https://www.opencart.com/index.php?rout ... on_id=7112
Re: Showing Products in Main Menu
Posted: Sun Jul 30, 2017 9:05 pm
by eddy-r3
hi IP_CAM, thanks for the replies and the suggested links, i did take a look through the links, and some of code and did actually make some progress!!
Im getting the error now on my menu :
Code: Select all
Notice: Undefined variable: products in C:\wamp64\www\iyouprint\catalog\view\theme\iyouprint\template\common\header.tpl on line 124
Warning: Invalid argument supplied for foreach() in C:\wamp64\www\iyouprint\catalog\view\theme\iyouprint\template\common\header.tpl on line 124
In my controller i have the following :
Code: Select all
$this->load->model('catalog/category'); //
$this->load->model('catalog/product'); //Load our models so the controller can get data
$categories = $this->model_catalog_category->getCategories(0); //get all top level categories
$all_products = array();
foreach ($categories as $category) //go through each category and get all the products for each category
{
$category_products = $this->model_catalog_product->getProductsforCategoryId($category['category_id']); //returns product IDs for category
foreach ($category_products as $category_product)
{
$product_data = $this->model_catalog_product->getProduct($category_product); //fetch product data for this product then add it to our array of all products
$all_products[] = array(
'href' => $this->url->link('product/product', 'product_id=' . $product_data['product_id']),
'name' => $product_data['name']
);
}
}
$this->data['products'] = $all_products; //Now pass our product array data to the view, in the view this will be the $products array
Re: Showing Products in Main Menu
Posted: Mon Jul 31, 2017 6:27 am
by eddy-r3
Ok so a bit more progress, any help would be great!
I've managed to get the products into the menu, the problem i have is showing products from each category, as it stands, each category is showing the 1 test product i have on the site, almost as if that product is assigned to every category, its not, its only assigned to one...
Any ideas?
header controller :
Code: Select all
// START PRODUCTS //
$data['products'] = array();
$results = $this->model_catalog_product->getProducts();
if ($results) {
foreach ($results as $result) {
$data['products'][] = array(
'product_id' => $result['product_id'],
'name' => $result['name'],
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'])
);
}
}
// END PRODUCTS //
header.tpl :
Code: Select all
<ul class="dropdown-menu mega-dropdown-menu row">
<?php foreach ($categories as $category) { ?>
<li class="col-sm-4">
<ul>
<li class="dropdown-header"><?php echo $category['name']; ?></li>
<?php foreach ($products as $product) { ?>
<li><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
Re: Showing Products in Main Menu
Posted: Mon Jul 31, 2017 4:03 pm
by artcore
I assume you mean to add the product array to the category array in the controller. That way products are grouped under the correct category id and available as $category['products']
Code: Select all
$data['categories'][] = array(
...snip
'products'=>$cat_products,//the array of products for each category
);
Code: Select all
<ul class="dropdown-menu mega-dropdown-menu row">
<?php foreach ($categories as $category) { ?>
<li class="col-sm-4">
<ul>
<li class="dropdown-header"><?php echo $category['name']; ?>
<ul class="dropdown-menu"><!--start product dropdown-->
<?php foreach ($category['products'] as $product) { ?>
<li><a href="<?php echo $product['href']; ?>"><?php echo $product['name']; ?></a></li>
</ul>
</li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
Looks like you want the products in a dropdown so you need some work on the html as well. I added a snippet but needs to be completed with the correct data-target properties. Bootstrap website has the info for this!
http://getbootstrap.com/javascript/#dropdowns
Re: Showing Products in Main Menu
Posted: Mon Jul 31, 2017 5:10 pm
by eddy-r3
Wow thanks so much for the reply!
I added the code you mentioned and now getting a different error :
Code: Select all
Notice: Undefined variable: cat_products in C:\wamp64\www\iyouprint\catalog\controller\common\header.php on line 139
Code: Select all
Warning: Invalid argument supplied for foreach() in C:\wamp64\www\iyouprint\catalog\view\theme\iyouprint\template\common\header.tpl on line 124
Thanks again!
Re: Showing Products in Main Menu
Posted: Mon Jul 31, 2017 5:11 pm
by eddy-r3
Thought this might help, this is the whole // MENU // section from my header controller :
Code: Select all
// Menu
$this->load->model('catalog/category');
$this->load->model('catalog/product');
// START PRODUCTS //
$data['products'] = array();
$results = $this->model_catalog_product->getProducts();
if ($results) {
foreach ($results as $result) {
$data['products'][] = array(
'product_id' => $result['product_id'],
'name' => $result['name'],
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'])
);
}
}
// END PRODUCTS //
$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']),
'products'=>$cat_products,//the array of products for each category
);
}
}
$data['language'] = $this->load->controller('common/language');
$data['currency'] = $this->load->controller('common/currency');
$data['search'] = $this->load->controller('common/search');
$data['cart'] = $this->load->controller('common/cart');
// For page specific css
if (isset($this->request->get['route'])) {
if (isset($this->request->get['product_id'])) {
$class = '-' . $this->request->get['product_id'];
} elseif (isset($this->request->get['path'])) {
$class = '-' . $this->request->get['path'];
} elseif (isset($this->request->get['manufacturer_id'])) {
$class = '-' . $this->request->get['manufacturer_id'];
} elseif (isset($this->request->get['information_id'])) {
$class = '-' . $this->request->get['information_id'];
} else {
$class = '';
}
$data['class'] = str_replace('/', '-', $this->request->get['route']) . $class;
} else {
$data['class'] = 'common-home';
}
return $this->load->view('common/header', $data);
}
Re: Showing Products in Main Menu
Posted: Mon Jul 31, 2017 6:05 pm
by artcore
See below.
$cat_products was just my name, you need to use the one you created.
I added the products inside the category loop otherwise you will get all the same products for each category.
This filters the products by cat id
array('filter_category_id'=>$category['category_id'])
This also limits results to 5
array('filter_category_id'=>$category['category_id'], 'limit'=>5)
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) {
// START PRODUCTS //
$results = $this->model_catalog_product->getProducts(array('filter_category_id'=>$category['category_id']));
//init products array for each cat
$cat_products = array();
if ($results) {
foreach ($results as $result) {
$cat_products[] = array(
'product_id' => $result['product_id'],
'name' => $result['name'],
'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'], true),
);
}
}
// END PRODUCTS //
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']),
'products' => $cat_products,//the array of products for each category
);
}
}
Re: Showing Products in Main Menu
Posted: Mon Jul 31, 2017 6:11 pm
by eddy-r3
Again, thanks so much for the reply! Really helping a lot!
So, although all of my errors are now gone, im back to a previous issue, where each category, has the same 1, test product listed under it.
My test product is "Sample Product"
My categories are 1/2/3/4
Sample product is only on category 1, but its showing as being part of 1/2/3/4
See screenshot for illustration, any ideas?
Is the the TPL file or the controller?
Re: Showing Products in Main Menu
Posted: Mon Jul 31, 2017 6:13 pm
by eddy-r3
Ignore my last message! Your latest snippet solved it!
Thankyou so much for the explanation!
Re: [SOLVED]Showing Products in Main Menu
Posted: Mon Jul 31, 2017 7:03 pm
by artcore
You're welcome, and it looks sharp too