Jump to content

Recommended Posts

Доброй ночи.
Проблема такова. на главной стоит модуль categoryhome

Нужно было добавить подкатегории, но при их выводе - выводяться все категории, а не для каждой родительской категории отдельно. В чем может быть проблема ?
Прикладываю контроллер и tpl модуля. post-705820-0-56165400-1454627011_thumb.png

 

Контроллер:

<?php  
class ControllerModulecategoryhome extends Controller {
	protected $category_id = 0;
	protected $path = array();
	
	protected function index($setting) {
		$this->language->load('module/categoryhome');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->load->model('catalog/category');
		
		$this->load->model('tool/image');
		$url = '';
		$this->data['categoryhome'] = $this->getCategories($this->config->get('categoryhome_category'));
						
		$this->id = 'categoryhome';
if (isset($this->request->get['path'])) {
			$parts = explode('_', $this->request->get['path']);
			$this->category_id = end($this->path);
		} else {
			$parts = array();
		}
		
		if (isset($parts[0])) {
			$this->data['category_id'] = $parts[0];
		} else {
			$this->data['category_id'] = 0;
		}
		
		if (isset($parts[1])) {
			$this->data['child_id'] = $parts[1];
		} else {
			$this->data['child_id'] = 0;
		}
							
		$this->load->model('catalog/category');

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

		$this->data['categories'] = array();

		$categories = $this->model_catalog_category->getCategories(0);
		
		//Показывать или нет количество товаров
		//$show_product_count = $this->config->get('config_product_count');

		foreach ($categories as $category) {
			//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
			//if ($show_product_count) {
			//	$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) {
				//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
				//if ($show_product_count) {
				//	$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'],
					'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'],
				'children'    => $children_data,
				'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
			);	
		}
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/categoryhome.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/categoryhome.tpl';
		} else {
			$this->template = 'default/template/module/categoryhome.tpl';
		}
		
		$this->render();
  	}
	
	protected function getCategories($parent_id, $current_path = '') {
		
		$categoryhome = array();
		$category_id = array_shift($this->path);
		
		$results = $this->model_catalog_category->getCategories($parent_id);
		
		$i=0;
		foreach ($results as $result) {	
			if (!$current_path) 
			{
				$new_path = $result['category_id'];
			} 
			else 
			{
				$new_path = $current_path . '_' . $result['category_id'];
			}
			
			
			
			
			if ($this->category_id == $result['category_id']) {
			$categoryhome[$i]['href'] = $this->url->link('product/category', 'path=' . $new_path);
			
			} else {
			$categoryhome[$i]['href'] =  $this->url->link('product/category', 'path=' . $new_path);
			}
			if ($result['image']) {
						$image = $result['image'];
					} else {
						$image = 'no_image.jpg';
					}
			$categoryhome[$i]['thumb'] = $this->model_tool_image->resize($image, 150, 150);
			$categoryhome[$i]['name'] = $result['name'];
			
        $i++;
 
		}
 
		
		return $categoryhome;
	}		
}
?>

и tpl:

<div class="box">
<div class="box-content">
<div class="box-cat">
  
<?php foreach ($categoryhome as $categoryhome) { ?>
<div>
      <div class="image"><a href="<?php echo $categoryhome['href']; ?>"><img src="<?php echo $categoryhome['thumb']; ?>" title="<?php echo $categoryhome['name']; ?>" alt="<?php echo $categoryhome['name']; ?>" /></a></div>
      <div class="name"><a href="<?php echo $categoryhome['href']; ?>"><?php echo $categoryhome['name']; ?></a></div>
	   <?php foreach ($categories as $category) { ?>
	    <?php if ($category['children']) { ?>
		<?php foreach ($category['children'] as $child) { ?>
		 <?php if ($child['category_id'] == $child_id) { ?>
            <a href="<?php echo $child['href']; ?>"> - <?php echo $child['name']; ?></a>
            <?php } else { ?>
            <a href="<?php echo $child['href']; ?>"> - <?php echo $child['name']; ?></a>
            <?php } ?>
			<?php } ?>
			<?php } ?>
			<?php } ?>
</div>
<?php } ?>
</div>
</div>
</div>
Edited by Wenceslaus

Share this post


Link to post
Share on other sites

1. /catalog/controller/module/categoryhome.php

<?php  
class ControllerModulecategoryhome extends Controller {
	protected $category_id = 0;
	protected $path = array();
	
	protected function index($setting) {
		$this->language->load('module/categoryhome');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->load->model('catalog/category');
		
		$this->load->model('tool/image');
		$url = '';
		$this->data['categoryhome'] = $this->getCategories($this->config->get('categoryhome_category'));
						
		$this->id = 'categoryhome';
if (isset($this->request->get['path'])) {
			$parts = explode('_', $this->request->get['path']);
			$this->category_id = end($this->path);
		} else {
			$parts = array();
		}
		
		if (isset($parts[0])) {
			$this->data['category_id'] = $parts[0];
		} else {
			$this->data['category_id'] = 0;
		}
		
		if (isset($parts[1])) {
			$this->data['child_id'] = $parts[1];
		} else {
			$this->data['child_id'] = 0;
		}
							
$this->load->model('catalog/product');

		$this->data['categories'] = array();

		$categories = $this->model_catalog_category->getCategories(0);
		
		//Показывать или нет количество товаров
		//$show_product_count = $this->config->get('config_product_count');

		foreach ($categories as $category) {
			//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
			//if ($show_product_count) {
			//	$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) {
				//Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать
				//if ($show_product_count) {
				//	$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'],
					'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'],
				'children'    => $children_data,
				'href'        => $this->url->link('product/category', 'path=' . $category['category_id'])
			);	
		}
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/categoryhome.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/categoryhome.tpl';
		} else {
			$this->template = 'default/template/module/categoryhome.tpl';
		}
		
		$this->render();
  	}
	
	protected function getCategories($parent_id, $current_path = '') {
		
		$categoryhome = array();
		$category_id = array_shift($this->path);
		
		$results = $this->model_catalog_category->getCategories($parent_id);
		$i=0;
		foreach ($results as $result) {	
			if (!$current_path) 
			{
				$new_path = $result['category_id'];
			} 
			else 
			{
				$new_path = $current_path . '_' . $result['category_id'];
			}
			
			
			
			
			if ($this->category_id == $result['category_id']) {
			$categoryhome[$i]['href'] = $this->url->link('product/category', 'path=' . $new_path);
			
			} else {
			$categoryhome[$i]['href'] =  $this->url->link('product/category', 'path=' . $new_path);
			}
			if ($result['image']) {
						$image = $result['image'];
					} else {
						$image = 'no_image.jpg';
					}
			$categoryhome[$i]['thumb'] = $this->model_tool_image->resize($image, 150, 150);
			$categoryhome[$i]['name'] = $result['name'];
			
        $i++;
		}
 
		
		return $categoryhome;
	}		
}
?>

2. /catalog/view/theme/ваша_тема/template/module/categoryhome.tpl

<div class="box">
<div class="box-content">
<div class="box-cat">
  
<?php foreach ($categoryhome as $cathome) { ?>
<div>
      <div class="image"><a href="<?php echo $cathome['href']; ?>"><img src="<?php echo $cathome['thumb']; ?>" itle="<?php echo $cathome['name']; ?>" alt="<?php echo $cathome['name']; ?>" /></a>
      </div>
      <div class="name"><a href="<?php echo $cathome['href']; ?>"><?php echo $cathome['name']; ?></a></div>

		   <?php foreach ($categories as $category) { 
		   if ($cathome['name'] == $category['name']) { ?>

			    <?php if ($category['children']) { ?>
				<ul class="box-category">
					<?php foreach ($category['children'] as $child) { ?>
					<li>
					 <?php if ($child['category_id'] == $child_id) { ?>
			            <a href="<?php echo $child['href']; ?>"> - <?php echo $child['name']; ?></a>
			            <?php } else { ?>
			            <a href="<?php echo $child['href']; ?>"> - <?php echo $child['name']; ?></a>
			            <?php } ?>
					</li>
						<?php } ?>
				</ul>
					<?php } ?>
					<?php } ?>

			<?php } ?>

</div>
<?php } ?>
</div>
</div>
</div>

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.