Jump to content
  • разработка интернет магазинов на opencart
  • доработка интернет магазинов на opencart

Сгделать меню кешируемым


 Share

Recommended Posts

Дописывал модуль под OC2 для вывода меню подкатегорий для заданной родительской категории.

Всё рилииет, но захотелось сгделать меню с кешем


Нашёл нечто похожее от @Yesvik для синдартного модуля категорий. Нашёл похожий метод в могделе админки.

 

Но у меня реализация отличается, вот код

Spoiler

 

<?php
class ControllerModuleCatbyparrent extends Controller {
	public function index($setting) {


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

		if (isset($this->request->get['path'])) {
			$parts = explode('_', (string)$this->request->get['path']);
		} else {
			$parts = array();
		}

		if (isset($parts[0])) {
			$data['category_id'] = $parts[0];
		} else {
			$data['category_id'] = 0;
		}

		if (isset($parts[1])) {
			$data['child_id'] = $parts[1];
		} else {
			$data['child_id'] = 0;
		}
		
		if (isset($parts[2])) {
			$data['child_id2'] = $parts[2];
		} else {
			$data['child_id2'] = 0;
		}		
		
		if (isset($parts[3])) {
			$data['child_id3'] = $parts[3];
		} else {
			$data['child_id3'] = 0;
		}

  	$data['title'] = html_entity_decode($setting['title'][$this->config->get('config_language_id')]['title'], ENT_QUOTES, 'UTF-8');	  
	$data['categories'] = array();
		
		$categories_1 = $this->model_catalog_category->getAllCategories($setting['parent']);

		foreach ($categories_1 as $category_1) {
			$level_2_data = array();

			$categories_2 = $this->model_catalog_category->getAllCategories($category_1['category_id']);

			foreach ($categories_2 as $category_2) {
				$level_3_data = array();

				$categories_3 = $this->model_catalog_category->getAllCategories($category_2['category_id']);

				foreach ($categories_3 as $category_3) {
					
					$level_4_data = array();

					$categories_4 = $this->model_catalog_category->getAllCategories($category_3['category_id']);

					foreach ($categories_4 as $category_4) {
						$level_4_data[] = array(
							'category_id' => $category_4['category_id'],
							'name' => $category_4['name'],
							'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'] . '_' . $category_3['category_id'] . '_' . $category_4['category_id'])
						);
	
					}

					$level_3_data[] = array(
						'category_id' => $category_3['category_id'],
						'name' => $category_3['name'],
						'children' => $level_4_data,
						'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'] . '_' . $category_3['category_id'])
					);
	
				}

					$level_2_data[] = array(
						'category_id' => $category_2['category_id'],
						'name'     => $category_2['name'],
						'children' => $level_3_data,
						'href'     => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'])
					);
			}

			$data['categories'][] = array(
				'category_id' => $category_1['category_id'],
				'name'     => $category_1['name'],
				'children' => $level_2_data,
				'href'     => $this->url->link('product/category', 'path=' . $category_1['category_id'])
			);
		}

	if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/cat-by-parrent.tpl')) {
		return $this->load->view($this->config->get('config_template') . '/template/module/cat-by-parrent.tpl',$data);
	} else {
		return $this->load->view('default/template/module/cat-by-parrent.tpl', $data);

}
}
}

 

 

Может быть подскажет кто-нибудь, как это лучше реализовать?

Edited by d2boy
ошибки
Link to comment
Share on other sites


1 година назад, d2boy сказав:

Может быть подскажет кто-нибудь, как это лучше реализовать?

Просто берёте $data['categories'] перед строками ренгдера и запихиваете в кеш с ключом соответствующим текуещёму запросу.

Соответственно, в начно файла проверяете по этому ключу есть ли оно уже в кеше.

 

$data['categories'] = $this->cache->get('category.menu.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (string)$this->request->get['path']);

if (empty($data['categories']) {
   /*
   * Код генерации меню
   */

   $this->cache->set('category.menu.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (string)$this->request->get['path'], $$data['categories']);
}

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/cat-by-parrent.tpl')) {
...

 

Link to comment
Share on other sites


On 11/24/2020 at 6:31 AM, Dotrox said:

Просто берёте $data['categories'] перед строками ренгдера и запихиваете в кеш с ключом соответствующим текуещёму запросу.

Соответственно, в начно файла проверяете по этому ключу есть ли оно уже в кеше.

 

$data['categories'] = $this->cache->get('category.menu.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (string)$this->request->get['path']);

if (empty($data['categories']) {
   /*
   * Код генерации меню
   */

   $this->cache->set('category.menu.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id') . '.' . (string)$this->request->get['path'], $$data['categories']);
}

if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/cat-by-parrent.tpl')) {
...

 

 

Огромное спасипотому что, подправил в паре мест и зарилиило

 

Spoiler
<?php
class ControllerModuleCatbyparrent extends Controller {
	public function index($setting) {


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

		if (isset($this->request->get['path'])) {
			$parts = explode('_', (string)$this->request->get['path']);
		} else {
			$parts = array();
		}

		if (isset($parts[0])) {
			$data['category_id'] = $parts[0];
		} else {
			$data['category_id'] = 0;
		}

		if (isset($parts[1])) {
			$data['child_id'] = $parts[1];
		} else {
			$data['child_id'] = 0;
		}
		
		if (isset($parts[2])) {
			$data['child_id2'] = $parts[2];
		} else {
			$data['child_id2'] = 0;
		}		
		
		if (isset($parts[3])) {
			$data['child_id3'] = $parts[3];
		} else {
			$data['child_id3'] = 0;
		}

  	$data['title'] = html_entity_decode($setting['title'][$this->config->get('config_language_id')]['title'], ENT_QUOTES, 'UTF-8');	  
	$data['categories'] = array();
	$data['categories'] = $this->cache->get('category.menu.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'));

	if (empty($data['categories'])) {
	
		$categories_1 = $this->model_catalog_category->getCategories($setting['parent']);

		foreach ($categories_1 as $category_1) {
			$level_2_data = array();

			$categories_2 = $this->model_catalog_category->getCategories($category_1['category_id']);

			foreach ($categories_2 as $category_2) {
				$level_3_data = array();

				$categories_3 = $this->model_catalog_category->getCategories($category_2['category_id']);

				foreach ($categories_3 as $category_3) {
					
					$level_4_data = array();

					$categories_4 = $this->model_catalog_category->getCategories($category_3['category_id']);

					foreach ($categories_4 as $category_4) {
						$level_4_data[] = array(
							'category_id' => $category_4['category_id'],
							'name' => $category_4['name'],
							'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'] . '_' . $category_3['category_id'] . '_' . $category_4['category_id'])
						);
	
					}

					$level_3_data[] = array(
						'category_id' => $category_3['category_id'],
						'name' => $category_3['name'],
						'children' => $level_4_data,
						'href' => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'] . '_' . $category_3['category_id'])
					);
	
				}

					$level_2_data[] = array(
						'category_id' => $category_2['category_id'],
						'name'     => $category_2['name'],
						'children' => $level_3_data,
						'href'     => $this->url->link('product/category', 'path=' . $category_1['category_id'] . '_' . $category_2['category_id'])
					);
			}

			$data['categories'][] = array(
				'category_id' => $category_1['category_id'],
				'name'     => $category_1['name'],
				'children' => $level_2_data,
				'href'     => $this->url->link('product/category', 'path=' . $category_1['category_id'])
			);
		}
		$this->cache->set('category.menu.' . $this->config->get('config_language_id') . '.' . (int)$this->config->get('config_store_id'), $data['categories']);
	}

	if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/cat-by-parrent.tpl')) {
		return $this->load->view($this->config->get('config_template') . '/template/module/cat-by-parrent.tpl',$data);
	} else {
		return $this->load->view('default/template/module/cat-by-parrent.tpl', $data);

}
}
}

 

 

Link to comment
Share on other sites


On 11/29/2020 at 12:14 AM, Dotrox said:

Зачем вам теперьь эи строка?

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

Вы в следуюещёй же строке перезаписываете значение.

 

Спасипотому что!

Да, надо выкинуть

Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

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.