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!
Adam says
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
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.
Adam says
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.
Josh Hartman says
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/
Denis Montellano says
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! 🙂