WarpConduit Computing

  • Quick Tips
  • Web Design & Development
  • WordPress Plugins
  • Home
  • Passphrase Generator
  • Password Generator
  • About
  • Contact

Automatic Category Images for OpenCart 1.4.9.3

February 1, 2011 by Josh Hartman

The changes set forth below will cause OpenCart to assign a random product image from the category as the category image if one has not already been set. If the category contains no products, no_image.jpg will still display.

Make the following changes around line 112 of catalog/controller/product/category.php.

foreach ($results as $result) {
	if ($result['image']) {
		$image = $result['image'];
	} else {
/* START AUTOMATIC CATEGORY IMAGES */
      		$catimg = $this->model_catalog_product->getRandomProductImageByCategoryId($result['category_id']);
      		if($catimg){
      			$image = $catimg['image'];
      		}else{
      			$image = 'no_image.jpg';
      		}
/* END AUTOMATIC CATEGORY IMAGES */
	}
	
	$this->data['categories'][] = array(
		'name'  => $result['name'],
		'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $this->request->get['path'] . '_' . $result['category_id'] . $url),
		'thumb' => $this->model_tool_image->resize($image, $this->config->get('config_image_category_width'), $this->config->get('config_image_category_height'))
	);
}

Add this function to catalog/model/catalog/product.php.

public function getRandomProductImageByCategoryId($category_id) {
	$sql = "SELECT p.image FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id = p2c.product_id) WHERE p.status = '1' AND p.image != '' AND p.date_available <= NOW() AND p2c.category_id = '" . (int)$category_id . "' ORDER BY RAND() LIMIT 1";
			
	$query = $this->db->query($sql);
									  
	return $query->row;
}

That’s it, enjoy your automatically generated and random category images!

Filed Under: Web Design & Development Tagged With: automatic, category, image, opencart, php, random

Comments

  1. Adam says

    March 1, 2011 at 2:47 AM

    DO NOT USE ORDER BY RAND()
    This is extremely inefficient and for stores with thousands of products this will be slow.

    Further explanation:
    http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/

    • Josh Hartman says

      March 11, 2011 at 3:51 PM

      I’ll have to do some research and benchmarking on RAND() and post about it, but an article from 2005 doesn’t hold much weight for me and in this case i don’t think it will slow down any OpenCart store that has organized their products, since it only uses RAND() to select one product from the products in one category, not one product from all the products. If someone had a category with thousands of products in it’s root (which would be very unorganized) then it might be slowed. Even if this is the case, assigning a category image would fix the slowness.

  2. Adam says

    March 1, 2011 at 2:49 AM

    I posted up a more efficient solution here: http://forum.opencart.com/viewtopic.php?f=121&t=24125

    The code is ugly, but I’m sure you can clean it up.

  3. Josh Hartman says

    April 2, 2011 at 6:26 PM

    After researching and benchmarking MySQL random row solutions I have decided that the code will remain as is. You can read about my conclusions here: http://www.warpconduit.net/2011/03/23/selecting-a-random-record-using-mysql-benchmark-results/

  4. Denis Montellano says

    September 12, 2011 at 6:05 AM

    Hi. Great post. My friends referred your blogs to me. It looks like everyone knows about it, just not me, until now. Definetely will read your other posts, too. Thank you for sharing with us. Take care. Recommend. Web Designer great site. Like it! 🙂

Connect

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • YouTube

Recent Posts

  • Extremely Useful Applications for Web Development and IT Tasks
  • Installing BookStack Wiki on cPanel Shared Hosting
  • Media (MIME) Type Reference List

Tags

automatic benchmark cbc cipher class comparisons cpanel credit memo css decrypt encrypt font gzip htaccess html image increment javascript jquery list magento mcrypt mysql number old opencart order php profiling random redirect repository rijndael shipment software strict ubuntu url wincachegrind windows windows 7 wordpress xampp xdebug xss

Blogroll

  • CodeIgniter
  • Fusion Forward
  • jQuery
  • Nettuts+
  • Smashing Magazine

© 2023 WarpConduit Computing. All Rights Reserved.