Post by alyosha » Tue Sep 08, 2009 1:48 am

Hi, I'm trying to make opencart do something simpler than it was designed to do. I think.

I would like a single-page shop, with the front page displaying a list of all categories, and below each category, a list of all products in that category including "click to buy" buttons. The entire contents of the shop are laid plain on a single page with no need to go deeper into individual product pages.

My problem is that on the front page, none of the category variables are available. Only the product variables are there.

Of course I know that I need to make the proper variables available in home.php for the home.tpl to see them, so I have looked through the category.php file for code to cannibalize.

And I got a little lost.

Since my desired final layout will be something like this:

CATEGORY1 TITLE AND DESCRIPTION
>Product1 title and description and photo
>Product2 title and description and photo
CATEGORY2 TITLE AND DESCRIPTION
>Product3 title and description and photo
>Product4 title and description and photo
etc.,

I think what I need is a new module or code snippet that makes a single giant array of every attribute of every product sorted by category (ie category[product][attribute] ) ... from which I can then cherry-pick data for my single page layout.

does anyone have anything like this already built (before I go and butcher me up some previously elegant code)?

Thanks!

Newbie

Posts

Joined
Mon Sep 07, 2009 7:03 pm

Post by alyosha » Wed Sep 09, 2009 5:04 am

OK maybe this isn't the best way of doing it, but maybe it is; anyway it seems to work for the moment. And in the hope that someone else may profit from my labors, here is what I've done.

I changed the catalog/controller/common/home.php file to read as follows:

Code: Select all

<?php  
class ControllerCommonHome extends Controller {
	public function index() {
		$this->language->load('common/home');
		
		$this->document->title = sprintf($this->language->get('title'), $this->config->get('config_store'));
		$this->document->description = $this->config->get('config_meta_description');
		$this->document->breadcrumbs = array();
      	$this->document->breadcrumbs[] = array(
        	'href'      => $this->url->http('common/home'),
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	);
		
		$this->load->model('catalog/product');
		$this->load->model('catalog/review');
		$this->load->model('tool/seo_url');

		$this->data['heading_title'] = sprintf($this->language->get('heading_title'), $this->config->get('config_store'));
		$this->data['welcome'] = html_entity_decode($this->config->get('config_welcome_' . $this->language->getId()));
		$this->data['text_latest'] = $this->language->get('text_latest');
		$this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');
		
		$this->load->helper('image');

		$this->load->model('catalog/category');
		
		// -- here begins the looping through the categories and then within each category,
		// -- looping through the products.  a new array called $concat is created, with
		// -- the format $concat[$j]['product'][$k]['product_id'].  $j and $k are the category
		// -- and product indices.  The description of the second category is therefore
		// -- $concat[1]['description'] while the price of the fourth product in the third
		// -- category is $concat[2]['product'][3]['price']
		
		$newcatnumber=0;
		foreach ($this->model_catalog_category->getCategories() as $catResult) {
			$concat[$newcatnumber] = array(
			'name' => $catResult['name'],
			'cat_id' => $catResult['category_id'],
			'description' => strip_tags($catResult['description'])
			);
			
			$newprodnumber=0;
			foreach ($this->model_catalog_product->getProductsByCategoryId($catResult['category_id']) as $prodResult) {
			if ($prodResult['image']) {
				$image = $prodResult['image'];
			} else {
				$image = 'no_image.jpg';
			}
			$rating = $this->model_catalog_review->getAverageRating($prodResult['product_id']);	
			$special = $this->model_catalog_product->getProductSpecial($prodResult['product_id']);
			
			
			if ($special) {
				$special = $this->currency->format($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
			} else {
				$special = FALSE;
			}		
				$concat[$newcatnumber]['product'][$newprodnumber] = array(
					'name'	  => $prodResult['name'],
					'model'	  => $prodResult['model'],
					'description'	=>	htmlspecialchars_decode($prodResult['description'], ENT_NOQUOTES),
					'rating'  => $rating,
					'stars'	  => sprintf($this->language->get('text_stars'), $rating),
					'thumb'	  => HelperImage::resize($image, 120, 120),
					'price'   => $this->currency->format($this->tax->calculate($prodResult['price'], $prodResult['tax_class_id'], $this->config->get('config_tax'))),
 					'special' => $special, 
					'href'	  => $this->model_tool_seo_url->rewrite($this->url->http('product/product&product_id=' . $prodResult['product_id'])),
					'product_id'    => $prodResult['product_id'],
					'href2'   => $this->url->http('checkout/cart')
				);

				$newprodnumber++;;
			}
			
			$newcatnumber++;
		}
		$this->data['concat'] = $concat;  // make $concat available to the common/home.tpl template
		$this->data['button_add_to_cart'] = $this->language->get('button_add_to_cart');

		$this->id       = 'content';
		$this->template = $this->config->get('config_template') . 'common/home.tpl';
		$this->layout   = 'common/layout';

		
		$this->render();
	}
}
?>
And then in my home.tpl file, I can use the new array $concat along with all the juicy data it contains to make whatever sort of layout I need.

If someone knows a better way to do this, I'm all ears. Likewise for potential bugs.

Newbie

Posts

Joined
Mon Sep 07, 2009 7:03 pm
Who is online

Users browsing this forum: No registered users and 6 guests