Post by keithspriggs » Mon Aug 04, 2014 9:20 am

Hi Guys,
A friend of mine has asked me to create an opencart site.
Everything is going fine
I have run into a problem on the home page.
The issue is that he wants one block to display the child of the category, which is no problem.
Job done there.

However when I try to get the child of the child, then it falls down.
here is the code i'm using and could anyone point out the major errors of my way.

Code: Select all


class ControllerCommonHome extends Controller {
	public function index() {
		$this->document->setTitle($this->config->get('config_title'));
		$this->document->setDescription($this->config->get('config_meta_description'));

        $this->load->model('catalog/category');
        $this->load->model('catalog/product');
        $this->load->model('tool/image');
        $this->data['categories'] = array();

        $categories = $this->model_catalog_category->getCategories(0);
        // Put category of tanks in the brackets above.

        foreach ($categories as $category) {
            $total = $this->model_catalog_product->getTotalProducts(array('filter_category_id' => $category['category_id']));

            $children_data = array();

            $children = $this->model_catalog_category->getCategories($category['category_id']);

            foreach ($children as $child) {
                $data = array(
                    'filter_category_id'  => $child['category_id'],
                    'filter_sub_category' => true
                );

                $product_total = $this->model_catalog_product->getTotalProducts($data);

                $total += $product_total;

                $children_data[] = array(
                    'category_id' => $child['category_id'],
                    'name'        => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
                    'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']) 
                );      
            }

            $this->data['categories'][] = array(
                'category_id' => $category['category_id'],
                'name'        => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $total . ')' : ''),
                'children'    => $children_data,
                'image'       => $this->model_tool_image->resize($category['image'], 180, 180),
                'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
            );  
        }
        
		$this->data['heading_title'] = $this->config->get('config_title');
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/home.tpl';
		} else {
			$this->template = 'default/template/common/home.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);
										
		$this->response->setOutput($this->render());
	}
}
?>

Newbie

Posts

Joined
Thu Jul 31, 2014 2:06 am

Post by andrei.bogdan » Mon Aug 04, 2014 6:55 pm

Open Cart support by default only 1-level children categories, so you will have to custom code a function for this. You can run the loop and find categories for each of the $child['category_id'] also and create a multidimensional array.

Andy

Andrei
andrei.opencart[at]yahoo.com
Themes and Extensions


Active Member

Posts

Joined
Mon Mar 25, 2013 4:52 am

Post by keithspriggs » Tue Aug 05, 2014 6:50 am

first of all, Andy thank you for your advice
Here is the code that I have worked on.
It doesn't work so I'm looking for some advice on the code
This is the code that I have written so far

Code: Select all

foreach($grandchildren as grandchild) {
					$data = array(
                    'filter_category_id'  => $grandchild['category_id'],
                    'filter_sub_category' => true
                );
				
				$grandchildren[] = array(
					'category_id' => $grandchild['category_id'],
                    'name'        => $grandchild['name'] ,
                    'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $grandchild['category_id']) 
				);
				
This is taken from the full code here

Code: Select all


<?php  
class ControllerCommonHome extends Controller {
	public function index() {
		$this->document->setTitle($this->config->get('config_title'));
		$this->document->setDescription($this->config->get('config_meta_description'));

        $this->load->model('catalog/category');
        $this->load->model('catalog/product');
        $this->load->model('tool/image');
        $this->data['categories'] = array();

        $categories = $this->model_catalog_category->getCategories(0);
        // Put category of tanks in the brackets above.

        foreach ($categories as $category) {
			$children_data = array();

            $children = $this->model_catalog_category->getCategories($category['category_id']);

            foreach ($children as $child) {
				$grandchildren_data = array();
				
				$grandchildren = $this->model_catalog_category->getCategories($category['category_id']);
				
				foreach($grandchildren as grandchild) {
					$data = array(
                    'filter_category_id'  => $grandchild['category_id'],
                    'filter_sub_category' => true
                );
				
				$grandchildren[] = array(
					'category_id' => $grandchild['category_id'],
                    'name'        => $grandchild['name'] ,
                    'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $grandchild['category_id']) 
				);
				
				}
			
                $data = array(
                    'filter_category_id'  => $child['category_id'],
                    'filter_sub_category' => true
                );
           
                $children_data[] = array(
                    'category_id' => $child['category_id'],
                    'name'        => $child['name'] ,
					'grandchildren' => $grandchildren_data,
                    'href'        => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']) 
                );      
            }

            $this->data['categories'][][] = array(
                'category_id' => $category['category_id'],
                'name'        => $category['name'] . ($this->config->get('config_product_count') ? ' (' . $total . ')' : ''),
                'children'    => $children_data,
                'image'       => $this->model_tool_image->resize($category['image'], 180, 180),
                'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
            );  
        }
        
		$this->data['heading_title'] = $this->config->get('config_title');
		
		
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/home.tpl';
		} else {
			$this->template = 'default/template/common/home.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);
										
		$this->response->setOutput($this->render());
	}
}
?>
By the way, I forgot to say, PHP is not my language of choice, in fact I have no programming knowledge of php other than mr. google and ms bing, so I would be grateful for any help.
Keith

Newbie

Posts

Joined
Thu Jul 31, 2014 2:06 am

Post by Opencart13 » Wed Mar 04, 2015 6:15 pm

Anyone has solved the problem of multiple levels on the categories??

in catalog > controller > module > category.php how can I build an array Categories like this example:

Array
(
[0] => Array
(
[category_id] => 1
[category_name] => Livello 0
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[category_id] = 2
[category_name] => Livello 1
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[category_id] = 3
[category_name] => Livello 2
[parent_id] => 2
[children] => Array
(
[0] => Array
(
[category_id] = 4
[category_name] => Livello 3
[parent_id] => 3
[children] => Array
....
....
....

How I can nest an unlimited number of parents and children ???

-livello 0
-livello 1
-livello 2
........

Please help!!!

Newbie

Posts

Joined
Wed Mar 04, 2015 5:57 pm
Who is online

Users browsing this forum: No registered users and 56 guests