Page 1 of 2

Latest Products Page [SOLVED]

Posted: Thu Apr 29, 2010 2:09 am
by kroozing
Hi all,

Just starting to play arround with open cart and things are pretty much straight forward however, one thing is making me scratch my head.
Is it possible to have a latest products page (like the specials page) showing the last few products.
I have searched and someone has asked the question but with no replies.

Thanks

Re: Latest Products Page

Posted: Thu Apr 29, 2010 2:30 am
by i2Paq
There is, by default, a Latest product module in a default 1.4.7 install.

Re: Latest Products Page

Posted: Thu Apr 29, 2010 2:33 am
by kroozing
Thanks for your reply,

I was talking about a full page not just a module. the specials is a page and a module.

Re: Latest Products Page

Posted: Thu Apr 29, 2010 2:36 am
by Qphoria
clone the specials page and make all references to "special" set to "latest"
then the only real change is the db query for
$this->model_catalog_product->getProductSpecials()
to
$this->model_catalog_product->getLatestProducts()

Re: Latest Products Page

Posted: Thu Apr 29, 2010 2:39 am
by kroozing
Thanks Qphoria, I'll give it a try now.

You've helped me loads just from reading your posts.

Re: Latest Products Page

Posted: Thu Apr 29, 2010 2:59 am
by kroozing
The link now works with no errors however, there are no products shown even thought the latest products module shows results.

Would it be something to do with $this->model_catalog_product->getTotalProductSpecials();

here is my controller/product/latest.php

Code: Select all

<?php 
class ControllerProductLatest extends Controller { 	
	public function index() { 
    	$this->language->load('product/latest');
	  	  
    	$this->document->title = $this->language->get('heading_title');

		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTP_SERVER . 'index.php?route=common/home',
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

		$url = '';
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}	

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}	
			
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTP_SERVER . 'index.php?route=product/latest' . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => $this->language->get('text_separator')
   		);
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
   
		$this->data['text_sort'] = $this->language->get('text_sort');
			 
  		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
	
		$this->load->model('catalog/product');
			
		$product_total = $this->model_catalog_product->getTotalProductSpecials();
						
		if ($product_total) {
			$url = '';
				
			$this->load->model('catalog/review');
			$this->load->model('tool/seo_url');
			$this->load->model('tool/image');
				
       		$this->data['products'] = array();
				
			$results = $this->model_catalog_product->getLatestProducts($sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));
        		
			foreach ($results as $result) {
				if ($result['image']) {
					$image = $result['image'];
				} else {
					$image = 'no_image.jpg';
				}						
					
				$rating = $this->model_catalog_review->getAverageRating($result['product_id']);	
									
				$this->data['products'][] = array(
           			'name'    => $result['name'],
					'model'   => $result['model'],
					'rating'  => $rating,
					'stars'   => sprintf($this->language->get('text_stars'), $rating),
           			'thumb'   => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
           			'price'   => $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
					'special' => $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))),
					'href'    => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product' . $url . '&product_id=' . $result['product_id'])
       			);
        	}

			if (!$this->config->get('config_customer_price')) {
				$this->data['display_price'] = TRUE;
			} elseif ($this->customer->isLogged()) {
				$this->data['display_price'] = TRUE;
			} else {
				$this->data['display_price'] = FALSE;
			}

			$url = '';

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}	
				
			$this->data['sorts'] = array();
				
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_name_asc'),
				'value' => 'pd.name',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=pd.name'
			); 

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_name_desc'),
				'value' => 'pd.name-DESC',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=pd.name&order=DESC'
			);  

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_price_asc'),
				'value' => 'latest-ASC',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=latest&order=ASC'
			); 

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_price_desc'),
				'value' => 'latest-DESC',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=latest&order=DESC'
			); 
				
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_rating_desc'),
				'value' => 'rating-DESC',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=rating&order=DESC'
			); 
				
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_rating_asc'),
				'value' => 'rating-ASC',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=rating&order=ASC'
			); 
				
			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
				
			$pagination = new Pagination();
			$pagination->total = $product_total;
			$pagination->page = $page;
			$pagination->limit = $this->config->get('config_catalog_limit');
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = HTTP_SERVER . 'index.php?route=product/latest' . $url . '&page={page}';
				
			$this->data['pagination'] = $pagination->render();
				
			$this->data['sort'] = $sort;
			$this->data['order'] = $order;

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/latest.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/product/latest.tpl';
			} else {
				$this->template = 'default/template/product/latest.tpl';
			}
			
			$this->children = array(
				'common/header',
				'common/footer',
				'common/column_left',
				'common/column_right'
			);
		
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));			
		} else {
      		$this->data['text_error'] = $this->language->get('text_empty');

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

      		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';
	  				
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/header',
				'common/footer',
				'common/column_left',
				'common/column_right'
			);	
			
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
		}
  	}
}
?>

Re: Latest Products Page

Posted: Thu Apr 29, 2010 3:11 am
by Qphoria
change
$results = $this->model_catalog_product->getLatestProducts($sort, $order, ($page - 1) * $this->config->get('config_catalog_limit'), $this->config->get('config_catalog_limit'));

to
$results = $this->model_catalog_product->getLatestProducts($this->config->get('config_catalog_limit'));

At this time, the LatestProducts function wasn't designed to have pagination.. just a single limit.

Re: Latest Products Page

Posted: Thu Apr 29, 2010 3:24 am
by kroozing
Thanks loads, that worked a treat.

I Seem to have a problem with line 90 saying that i have Undefined index: special

'special' => $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax'))),

If tried the code from the module but the error still shows with no special price

Many thanks

Re: Latest Products Page

Posted: Thu Apr 29, 2010 4:29 am
by kroozing
I have managed to fix it, For anyone else wanting a latest products page i have attached the code needed.

First file catalog/controller/product/latest.php

Code: Select all

<?php 
class ControllerProductLatest extends Controller { 	
	public function index() { 
    	$this->language->load('product/latest');
	  	  
    	$this->document->title = $this->language->get('heading_title');

		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTP_SERVER . 'index.php?route=common/home',
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

		$url = '';
		
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}	

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
				
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}	
			
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTP_SERVER . 'index.php?route=product/latest' . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => $this->language->get('text_separator')
   		);
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
   
		$this->data['text_sort'] = $this->language->get('text_sort');
			 
  		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
	
		$this->load->model('catalog/product');
			

			$url = '';
				
			$this->load->model('catalog/review');
			$this->load->model('tool/seo_url');
			$this->load->model('tool/image');
				
       		$this->data['products'] = array();
				
			$results = $this->model_catalog_product->getLatestProducts($this->config->get('config_catalog_limit'));

        		
			foreach ($results as $result) {
			if ($result['image']) {
				$image = $result['image'];
			} else {
				$image = 'no_image.jpg';
			}
			
			$rating = $this->model_catalog_review->getAverageRating($result['product_id']);	

			$special = FALSE;
			
			$discount = $this->model_catalog_product->getProductDiscount($result['product_id']);
			
			if ($discount) {
				$price = $this->currency->format($this->tax->calculate($discount, $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
			
				$special = $this->model_catalog_product->getProductSpecial($result['product_id']);
			
				if ($special) {
					$special = $this->currency->format($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
				}						
			}
			
			$this->data['products'][] = array(											  
				'name'    => $result['name'],
				'price'   => $price,
				'special' => $special,
				'thumb'   => $this->model_tool_image->resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height')),
				'href'    => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $result['product_id'])
			);
		}


			if (!$this->config->get('config_customer_price')) {
				$this->data['display_price'] = TRUE;
			} elseif ($this->customer->isLogged()) {
				$this->data['display_price'] = TRUE;
			} else {
				$this->data['display_price'] = FALSE;
			}

			$url = '';

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}	
				
			$this->data['sorts'] = array();
				
			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_name_asc'),
				'value' => 'pd.name',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=pd.name'
			); 

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_name_desc'),
				'value' => 'pd.name-DESC',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=pd.name&order=DESC'
			);  

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_price_asc'),
				'value' => 'latest-ASC',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=latest&order=ASC'
			); 

			$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_price_desc'),
				'value' => 'latest-DESC',
				'href'  => HTTP_SERVER . 'index.php?route=product/latest' . $url . '&sort=latest&order=DESC'
			); 
				 
				
			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}	

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
				
			//$pagination = new Pagination();
			//$pagination->total = $product_total;
			//$pagination->page = $page;
			//$pagination->limit = $this->config->get('config_catalog_limit');
			//$pagination->text = $this->language->get('text_pagination');
			//$pagination->url = HTTP_SERVER . 'index.php?route=product/latest' . $url . '&page={page}';
				
			//$this->data['pagination'] = $pagination->render();
				
			$this->data['sort'] = $sort;
			$this->data['order'] = $order;

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/latest.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/product/latest.tpl';
			} else {
				$this->template = 'default/template/product/latest.tpl';
			}
			
			$this->children = array(
				'common/header',
				'common/footer',
				'common/column_left',
				'common/column_right'
			);
		
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));			
		
  	}
}
?>
The second file /catalog/language/english/product/latest.php

Code: Select all

<?php
// Heading
$_['heading_title']     = 'Latest products';

// Text
$_['text_empty']        = 'There are no new products to list.';
$_['text_sort']         = 'Sort By:';
$_['text_name_asc']     = 'Name A - Z';
$_['text_name_desc']    = 'Name Z - A';
$_['text_price_asc']    = 'Price Low > High';
$_['text_price_desc']   = 'Price High < Low';

?>
And the 3rd file /catalog/view/theme/default/template/product/latest.tpl

Code: Select all

<?php echo $header; ?><?php echo $column_left; ?><?php echo $column_right; ?>
<div id="content">
  <div class="top">
    <div class="left"></div>
    <div class="right"></div>
    <div class="center">
      <h1><?php echo $heading_title; ?></h1>
    </div>
  </div>
  <div class="middle">
    <div class="sort">
      <div class="div1">
        <select name="sort" onchange="location = this.value">
          <?php foreach ($sorts as $sorts) { ?>
          <?php if (($sort . '-' . $order) == $sorts['value']) { ?>
          <option value="<?php echo str_replace('&', '&', $sorts['href']); ?>" selected="selected"><?php echo $sorts['text']; ?></option>
          <?php } else { ?>
          <option value="<?php echo str_replace('&', '&', $sorts['href']); ?>"><?php echo $sorts['text']; ?></option>
          <?php } ?>
          <?php } ?>
        </select>
      </div>
      <div class="div2"><?php echo $text_sort; ?></div>
    </div>
    <table class="list">
      <?php for ($i = 0; $i < sizeof($products); $i = $i + 4) { ?>
      <tr>
        <?php for ($j = $i; $j < ($i + 4); $j++) { ?>
        <td width="25%"><?php if (isset($products[$j])) { ?>
          <a href="<?php echo str_replace('&', '&', $products[$j]['href']); ?>"><img src="<?php echo $products[$j]['thumb']; ?>" title="<?php echo $products[$j]['name']; ?>" alt="<?php echo $products[$j]['name']; ?>" /></a><br />
          <a href="<?php echo str_replace('&', '&', $products[$j]['href']); ?>"><?php echo $products[$j]['name']; ?></a><br />
          <?php if ($display_price) { ?>
          <?php if (!$products[$j]['special']) { ?>
          <span style="color: #900; font-weight: bold;"><?php echo $products[$j]['price']; ?></span><br />
          <?php } else { ?>
          <span style="color: #900; font-weight: bold; text-decoration: line-through;"><?php echo $products[$j]['price']; ?></span> <span style="color: #F00;"><?php echo $products[$j]['special']; ?></span>
          <?php } ?>
          <?php } ?>
          <?php } ?></td>
        <?php } ?>
      </tr>
      <?php } ?>
    </table>
  </div>
  <div class="bottom">
    <div class="left"></div>
    <div class="right"></div>
    <div class="center"></div>
  </div>
</div>
<?php echo $footer; ?> 

Re: Latest Products Page [SOLVED]

Posted: Thu May 06, 2010 6:15 pm
by amertad
sample? or package!?!?

Re: Latest Products Page [SOLVED]

Posted: Mon Aug 02, 2010 2:26 am
by sjp1170
Hi

Fantastic mod and just what i was looking for. However i need to alter it slightly so that it will show all my latest products and not just 20. How can this be done?

Thanks

Re: Latest Products Page [SOLVED]

Posted: Thu Dec 09, 2010 1:09 am
by Simple
Is there anymore info on getting this on your store. I have added the files but cant get it to show on the homepage.
Thanks

Re: Latest Products Page [SOLVED]

Posted: Thu Dec 16, 2010 12:00 am
by engchun
opencart version V1.4.9.1 ,
under latest product module can i sort by categories and install 3 latest product module at home and sort by 3 different categories ? your reply is appreciated

Re: Latest Products Page [SOLVED]

Posted: Thu Dec 16, 2010 12:38 am
by Simple
engchum I installed this.... http://www.opencart.com/index.php?route ... 050&page=4

Well worth the money :)

Re: Latest Products Page [SOLVED]

Posted: Thu Jan 27, 2011 7:57 pm
by gennady
Please help update code latest.php for old version 1.3.2
1. replace

Code: Select all

'href'      => HTTP_SERVER . 'index.php?route 
with

Code: Select all

'href'      => $this->url->http('
2.

Code: Select all

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

Code: Select all

	$this->data['latest'] = array();
how to update code latest.php from the module latest product FIDO-X.NET for the controller/product

Code: Select all

<?php
// Latest Products Module by Fido-X (http://www.fido-x.net)
class ControllerModuleLatest extends Controller {
	protected function index() {
		$this->load->language('module/latest');

		if ($this->config->get('latest_position') == 'homepage') {
			$this->data['homepage'] = 'TRUE';
		}

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

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

		$this->load->helper('image');

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

		foreach ($this->model_catalog_product->getLatestProducts($this->config->get('latest_limit')) as $result) {
			if ($result['image']) {
				$image = $result['image'];
			} else {
				$image = 'no_image.jpg';
			}

			if ($this->config->get('latest_position') == 'homepage') {
				$thumb = image_resize($image, $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
			} else {
				$thumb = image_resize($image, 38, 38);
			}

			$rating = $this->model_catalog_review->getAverageRating($result['product_id']);

			$special = FALSE;

			$discount = $this->model_catalog_product->getProductDiscount($result['product_id']);

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

				$special = $this->model_catalog_product->getProductSpecial($result['product_id']);

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

			$this->data['latest'][] = array(
				'name'    => $result['name'],
				'model'   => $result['model'],
				'rating'  => $rating,
				'stars'   => sprintf($this->language->get('text_stars'), $rating),
				'thumb'   => $thumb,
				'price'   => $price,
				'special' => $special,
				'href'    => $this->model_tool_seo_url->rewrite($this->url->http('product/product&product_id=' . $result['product_id']))
			);
		}

		if (!$this->config->get('config_customer_price')) {
			$this->data['display_price'] = TRUE;
		} elseif ($this->customer->isLogged()) {
			$this->data['display_price'] = TRUE;
		} else {
			$this->data['display_price'] = FALSE;
		}

		$this->id       = 'latest';
		$this->template = $this->config->get('config_template') . 'module/latest.tpl';
		$this->render();
	}
}
?>


Re: Latest Products Page [SOLVED]

Posted: Wed Apr 20, 2011 4:26 am
by adriankoooo
So many thank to you! Exactly what I am looked for and working!

Re: Latest Products Page [SOLVED]

Posted: Thu Apr 21, 2011 7:50 pm
by ebudowanie
hi,

I do this all in upper posts and i have some errors in my shop:

http://www.sklep.e-budowanie.com/index. ... uct/latest

and i have a some questions:
- how to display all products or concrete number products ?
- how to display bestsellers in one page ?
- how to display featured in one page ?


I need some help :)

thanks for helps



----------edit------------

my opencart version 1.4.9.4

Re: Latest Products Page [SOLVED]

Posted: Tue Apr 26, 2011 3:20 pm
by ebudowanie
anyone help me ?

Re: Latest Products Page [SOLVED]

Posted: Thu Sep 22, 2011 11:01 pm
by santoshm
I am getting this error:
Fatal error: Cannot access private property Document::$title in D:\wamp\www\opencart\catalog\controller\product\latest.php on line 6

Re: Latest Products Page [SOLVED]

Posted: Fri Sep 30, 2011 1:22 am
by suntzu
There is a problem with the pagination. It does not exist. Do you think you could offer a solution for that?