Page 1 of 1

Category name in latest.tpl

Posted: Fri Jul 22, 2016 11:23 pm
by Ladi
Hello,
I try to add Category name in Latest product list on homepage (latest.tpl) with this code (found on web), but unsuccessfully.

With "category" part of code website is broken (show blank page). Without work ok.

What is done wrong?

Thanks for any kind of help.

Code in latest.php
...

Code: Select all

$categories = $this->model_catalog_product->getCategories($result['product_id']);
				if($categories){
				    $categories_info = $this->model_catalog_category->getCategory($categories[0]['category_id']);
				    $category_title = $categories_info['name'];
				}else{
				    $category_title = '';
				}

				$data['products'][] = array(
					'product_id'  => $result['product_id'],
					'thumb'       => $image,
					'manufacturer' => html_entity_decode($product_info['manufacturer'], ENT_QUOTES, 'UTF-8'),					
					'manufacturers' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id']),
					'category_title' => $category_title,
					'name'        => $result['name'],
					'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
					'price'       => $price,
					'special'     => $special,
					'tax'         => $tax,
					'rating'      => $rating,
					'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id']),
				);
...

Code in latest.tpl
...

Code: Select all

      <div><?php echo $product['category_title']; ?></div>
...

Re: Category name in latest.tpl

Posted: Fri Jul 22, 2016 11:48 pm
by straightlight
Followed is for demonstration purposes. In your catalog/controller/module/latest.php file,

find:

Code: Select all

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

Code: Select all

$this->load->model('catalog/category');
Then, find:

Code: Select all

if ($results) {
add right below:

Code: Select all

$i = 0;
Then, find:

Code: Select all

}

			return $this->load->view('module/latest', $data);
replace with:

Code: Select all

++$i;
}

			return $this->load->view('module/latest', $data);
Then, find:

Code: Select all

$data['products'][] = array(
add above:

Code: Select all

$products_to_categories = $this->model_catalog_product->getCategories($result['product_id']);

$category_titles = array();

$j = 0;

foreach ($products_to_categories as $category) {
    $category_info = $this->model_catalog_category->getCategory($category['category_id']);

    if (!empty($category_info['name'])) {
        $category_titles[$j] = html_entity_decode($category_info['name'], ENT_QUOTES, 'UTF-8');
    }

    ++$j;
}
Then, find:

Code: Select all

'thumb'       => $image,
add right below:

Code: Select all

'category_title' => (!empty($category_titles[$i]) ? $category_titles[$i] : ''),
Then, in your theme, you could use:

Code: Select all

<?php if ($product['category_title']) { ?>
    <?php echo $product['category_title']; ?>
<?php } ?>
inside the product loop.

Re: Category name in latest.tpl

Posted: Tue Jul 26, 2016 5:44 pm
by Ladi
Hi straightlight,

Thank you for helping!

Obvious I`m doing something wrong (still green with OC and PHP). Still does not work :/

Here is complete latest.php code...

Code: Select all

<?php
class ControllerModuleLatest extends Controller {
	public function index($setting) {
		$this->load->language('module/latest');

		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_tax'] = $this->language->get('text_tax');

		$data['button_cart'] = $this->language->get('button_cart');
		$data['button_wishlist'] = $this->language->get('button_wishlist');
		$data['button_compare'] = $this->language->get('button_compare');

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

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

		$this->load->model('tool/image');

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

		$filter_data = array(
			'sort'  => 'p.date_added',
			'order' => 'DESC',
			'start' => 0,
			'limit' => $setting['limit']
		);

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

		if ($results) {

			$i = 0; //new part

			foreach ($results as $result) {
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $setting['width'], $setting['height']);
				} else {
					$image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
				}

				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$price = false;
				}

				if ((float)$result['special']) {
					$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$special = false;
				}

				if ($this->config->get('config_tax')) {
					$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
				} else {
					$tax = false;
				}

				if ($this->config->get('config_review_status')) {
					$rating = $result['rating'];
				} else {
					$rating = false;
				}

				$product_info = $this->model_catalog_product->getProduct($result['product_id']);

				//new part start here

				$products_to_categories = $this->model_catalog_product->getCategories($result['product_id']);

				$category_titles = array();

					$j = 0;

					foreach ($products_to_categories as $category) {
					    $category_info = $this->model_catalog_category->getCategory($category['category_id']);

					    if (!empty($category_info['name'])) {
					        $category_titles[$j] = html_entity_decode($category_info['name'], ENT_QUOTES, 'UTF-8');
					    }

					    ++$j;
				}

				//new part end here

				$data['products'][] = array(
					'product_id'  => $result['product_id'],
					'thumb'       => $image,
					'category_title' => (!empty($category_titles[$i]) ? $category_titles[$i] : ''),
					'manufacturer' => html_entity_decode($product_info['manufacturer'], ENT_QUOTES, 'UTF-8'),					
					'manufacturers' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id']),
					'category_title' => $category_title,
					'name'        => $result['name'],
					'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
					'price'       => $price,
					'special'     => $special,
					'tax'         => $tax,
					'rating'      => $rating,
					'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id']),
				);
			}

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/latest.tpl')) {
				return $this->load->view($this->config->get('config_template') . '/template/module/latest.tpl', $data);

				++$i; //new part

			} else {
				return $this->load->view('default/template/module/latest.tpl', $data);
			}
		}
	}
}
Best Regards!

PS
OC is 2.0.3.1 version. I forgot to mention that

Re: Category name in latest.tpl

Posted: Tue Jul 26, 2016 6:42 pm
by straightlight
Ladi wrote:Hi straightlight,

Thank you for helping!

Obvious I`m doing something wrong (still green with OC and PHP). Still does not work :/

Here is complete latest.php code...

Code: Select all

<?php
class ControllerModuleLatest extends Controller {
	public function index($setting) {
		$this->load->language('module/latest');

		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_tax'] = $this->language->get('text_tax');

		$data['button_cart'] = $this->language->get('button_cart');
		$data['button_wishlist'] = $this->language->get('button_wishlist');
		$data['button_compare'] = $this->language->get('button_compare');

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

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

		$this->load->model('tool/image');

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

		$filter_data = array(
			'sort'  => 'p.date_added',
			'order' => 'DESC',
			'start' => 0,
			'limit' => $setting['limit']
		);

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

		if ($results) {

			$i = 0; //new part

			foreach ($results as $result) {
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $setting['width'], $setting['height']);
				} else {
					$image = $this->model_tool_image->resize('placeholder.png', $setting['width'], $setting['height']);
				}

				if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
					$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$price = false;
				}

				if ((float)$result['special']) {
					$special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$special = false;
				}

				if ($this->config->get('config_tax')) {
					$tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
				} else {
					$tax = false;
				}

				if ($this->config->get('config_review_status')) {
					$rating = $result['rating'];
				} else {
					$rating = false;
				}

				$product_info = $this->model_catalog_product->getProduct($result['product_id']);

				//new part start here

				$products_to_categories = $this->model_catalog_product->getCategories($result['product_id']);

				$category_titles = array();

					$j = 0;

					foreach ($products_to_categories as $category) {
					    $category_info = $this->model_catalog_category->getCategory($category['category_id']);

					    if (!empty($category_info['name'])) {
					        $category_titles[$j] = html_entity_decode($category_info['name'], ENT_QUOTES, 'UTF-8');
					    }

					    ++$j;
				}

				//new part end here

				$data['products'][] = array(
					'product_id'  => $result['product_id'],
					'thumb'       => $image,
					'category_title' => (!empty($category_titles[$i]) ? $category_titles[$i] : ''),
					'manufacturer' => html_entity_decode($product_info['manufacturer'], ENT_QUOTES, 'UTF-8'),					
					'manufacturers' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id']),
					'category_title' => $category_title,
					'name'        => $result['name'],
					'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
					'price'       => $price,
					'special'     => $special,
					'tax'         => $tax,
					'rating'      => $rating,
					'href'        => $this->url->link('product/product', 'product_id=' . $result['product_id']),
				);
			}

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/latest.tpl')) {
				return $this->load->view($this->config->get('config_template') . '/template/module/latest.tpl', $data);

				++$i; //new part

			} else {
				return $this->load->view('default/template/module/latest.tpl', $data);
			}
		}
	}
}
Best Regards!

PS
OC is 2.0.3.1 version. I forgot to mention that
You've put 'category_title' lines twice in the array ... my instructions above only indicates to put it once.

Re: Category name in latest.tpl

Posted: Tue Jul 26, 2016 7:47 pm
by Ladi
Sorry, my bad. Totally overlooked that.

Thank you one more time.

Best Regards!

Re: Category name in latest.tpl

Posted: Tue Jul 26, 2016 7:47 pm
by straightlight
Ladi wrote:Sorry, my bad. Totally overlooked that.

Thank you one more time.

Best Regards!
No problem, enjoy.

Re: Category name in latest.tpl

Posted: Fri Mar 24, 2017 3:40 am
by icky
It's not working on Opencart v2.3.0.2. :'(
Any idea how to edit that code to make it work on newest relase on default template?
Is there any extension for that?
I was looking for that everywhere but nothing works so far.

Re: Category name in latest.tpl

Posted: Sat Mar 25, 2017 7:43 am
by opencartboost
icky wrote:
Fri Mar 24, 2017 3:40 am
It's not working on Opencart v2.3.0.2. :'(
Any idea how to edit that code to make it work on newest relase on default template?
Is there any extension for that?
I was looking for that everywhere but nothing works so far.
I have one, i made for a client.

Re: Category name in latest.tpl

Posted: Sat Mar 25, 2017 5:40 pm
by icky
Thanks for reply Opencartboost! Can you provide link to that extension or post solution, eventualy an advice how you achieved that?

Re: Category name in latest.tpl

Posted: Sun Mar 26, 2017 11:49 am
by opencartboost
Demo here :
http://trial.opencartboost.com/
I am develop it using code from author module.

Re: Category name in latest.tpl

Posted: Tue Mar 28, 2017 2:08 am
by icky
Can you help me edit that code?
What i achieved is just a name of a first category (from couple) and no link to that category page.

What i did:
After $this->load->model('catalog/product');

Code: Select all

$this->load->model('catalog/category');
Before $data['products'][] = array(

Code: Select all

    $categories = $this->model_catalog_product->getCategories($result['product_id']);
	if($categories){
		$categories_info = $this->model_catalog_category->getCategory($categories[0]['category_id']);
		$category_title = $categories_info['name'];
	}else{
		$category_title = '';
	}
After 'name' => $result['name'],

Code: Select all

'category_title' => $category_title,
In latest.tpl

Code: Select all

<?php echo $product['category_title']; ?>
Like i said before now i have got only first category name.
What i want to achieve is to show all categories of product with links directing to that specific categories. (same as opencartboost's http://trial.opencartboost.com/).

I know that it's not that hard like I think but it gave me a headaches for couple of days.
Anyone?