Post by Ladi » Fri Jul 22, 2016 11:23 pm

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>
...
Last edited by straightlight on Fri Jul 22, 2016 11:33 pm, edited 1 time in total.
Reason: Added code tags with posted codes.

Newbie

Posts

Joined
Thu Jul 21, 2016 10:17 pm

Post by straightlight » Fri Jul 22, 2016 11:48 pm

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Ladi » Tue Jul 26, 2016 5:44 pm

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

Newbie

Posts

Joined
Thu Jul 21, 2016 10:17 pm

Post by straightlight » Tue Jul 26, 2016 6:42 pm

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.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by Ladi » Tue Jul 26, 2016 7:47 pm

Sorry, my bad. Totally overlooked that.

Thank you one more time.

Best Regards!

Newbie

Posts

Joined
Thu Jul 21, 2016 10:17 pm

Post by straightlight » Tue Jul 26, 2016 7:47 pm

Ladi wrote:Sorry, my bad. Totally overlooked that.

Thank you one more time.

Best Regards!
No problem, enjoy.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by icky » 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.

User avatar
Newbie

Posts

Joined
Fri Mar 24, 2017 3:34 am

Post by opencartboost » Sat Mar 25, 2017 7:43 am

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.

Active Member

Posts

Joined
Thu Jul 09, 2015 5:59 am

Post by icky » Sat Mar 25, 2017 5:40 pm

Thanks for reply Opencartboost! Can you provide link to that extension or post solution, eventualy an advice how you achieved that?

User avatar
Newbie

Posts

Joined
Fri Mar 24, 2017 3:34 am

Post by opencartboost » Sun Mar 26, 2017 11:49 am

Demo here :
http://trial.opencartboost.com/
I am develop it using code from author module.

Active Member

Posts

Joined
Thu Jul 09, 2015 5:59 am

Post by icky » Tue Mar 28, 2017 2:08 am

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?

User avatar
Newbie

Posts

Joined
Fri Mar 24, 2017 3:34 am
Who is online

Users browsing this forum: No registered users and 29 guests