Post by AdamN » Tue Dec 14, 2010 4:42 pm

My modification gets a random product image for the category and uses it as the category image when the category image is not set. It still uses the category image if that has been set by the user.

I did not use the mysql random sort as it proved way too slow when you have a large number or products. The randomization occurs in php code.

/catalog/controller/product/category.php
lines 62-66 before:

Code: Select all

if ($category_info['image']) {
				$image = $category_info['image'];
			} else {
				$image = '';
			}
after:

Code: Select all

if ($category_info['image']) {
				$image = $category_info['image'];
			} else {
				$image = '';
				// Use random image from products in the category
				$rndImgQry = "SELECT p.image AS image FROM product AS p JOIN product_to_category AS c ON p.product_id = c.product_id "
						. "WHERE c.category_id = " . $category_info['category_id'] . " AND p.image NOT LIKE 'no_image.jpg'";
				$rndImgRslt = mysql_query($rndImgQry) or die (mysql_error());
				if (mysql_num_rows($rndImgRslt) > 0)
				{
					$imgRows = array();
					while($imgRow = mysql_fetch_array($rndImgRslt))
					{
						$imgRows[] = $imgRow['image'];
					}
					$image = $imgRows[array_rand($imgRows)];
				}
			}
/catalog/controller/product/category.php
lines 109-113 before:

Code: Select all

if ($result['image']) {
						$image = $result['image'];
					} else {
						$image = 'no_image.jpg';
					}
after:

Code: Select all

if ($result['image']) {
						$image = $result['image'];
					} else {
						$image = 'no_image.jpg';
						// Use random image from products in the category
						$rndImgQry = "SELECT p.image AS image FROM product AS p JOIN product_to_category AS c ON p.product_id = c.product_id "
						       		. "WHERE c.category_id = " . $result['category_id'] . " AND p.image NOT LIKE 'no_image.jpg'";
						$rndImgRslt = mysql_query($rndImgQry) or die (mysql_error());
						if (mysql_num_rows($rndImgRslt) > 0)
						{
							$imgRows = array();
							while($imgRow = mysql_fetch_array($rndImgRslt))
							{
								$imgRows[] = $imgRow['image'];
							}
							$image = $imgRows[array_rand($imgRows)];
						}
					}

New member

Posts

Joined
Mon Oct 18, 2010 11:39 am

User avatar
Active Member

Posts

Joined
Mon May 16, 2011 7:24 pm
Location - UK

Post by AdamN » Sat Jun 11, 2011 11:54 pm

Thanks. Any suggestions for improvements?

New member

Posts

Joined
Mon Oct 18, 2010 11:39 am

Post by fsit » Tue Aug 16, 2011 2:36 am

Which version is it for?

New member

Posts

Joined
Mon Jul 25, 2011 12:44 am

Post by Derrickyoung95 » Mon Mar 12, 2012 1:25 am

I am going to assume this for 1.4.x as in my 1.5.3 installation most of the code does not exist as it is shown..

Has anyone got this to work in 1.5.x? Or know of another way to select a random product image to use as the category image if none defined


Posts

Joined
Mon Sep 05, 2011 8:10 pm

Post by AdamN » Mon Mar 12, 2012 12:48 pm

Yes it's for 1.4.x. Is there any demand for 1.5.x for this? I don't think many people are using it.

New member

Posts

Joined
Mon Oct 18, 2010 11:39 am

Post by AdamN » Mon Mar 12, 2012 12:48 pm

I can make a vqmod for 1.4.x as well if anyone wants that.

New member

Posts

Joined
Mon Oct 18, 2010 11:39 am

Post by s31teg » Tue Mar 20, 2012 8:07 pm

aye if you would not a a vqmod out for the latest version that would be great...

New member

Posts

Joined
Wed Jun 22, 2011 3:39 am

Post by AdamN » Wed Mar 21, 2012 10:02 am

I've attached a vqmod file for OpenCart 1.4.x. Can someone test it for me and let me know if it works. It's my first vqmod and I no longer have any 1.4.x stores to test it on.
Once it works, I'll do the 1.5.x one.

Attachments


New member

Posts

Joined
Mon Oct 18, 2010 11:39 am

Post by devplus » Thu Jul 05, 2012 10:27 pm

Any vqmod version fot 1.5.3.1??

New member

Posts

Joined
Fri May 18, 2012 11:43 pm

Post by AdamN » Thu Jul 05, 2012 10:43 pm

Hmmm. Give me a couple of weeks

New member

Posts

Joined
Mon Oct 18, 2010 11:39 am

Post by devplus » Mon Jul 30, 2012 11:38 pm

Hi AdamN,

any update on this one?

Cheers!

New member

Posts

Joined
Fri May 18, 2012 11:43 pm

Post by mrjave » Mon Aug 13, 2012 4:23 pm

me too AdamN,

it would be so kind of you if you could assist us in getting a VQMOD for V1.5.3.1.

many thanks in advance.

Jave

Active Member

Posts

Joined
Wed Jun 13, 2012 4:22 pm

Post by mnu59 » Sun May 03, 2020 9:40 am

AdamN wrote:
Tue Dec 14, 2010 4:42 pm

/catalog/controller/product/category.php
lines 62-66 before:

Code: Select all

if ($category_info['image']) {
				$image = $category_info['image'];
			} else {
				$image = '';
			}
after:

Code: Select all

if ($category_info['image']) {
				$image = $category_info['image'];
			} else {
				$image = '';
				// Use random image from products in the category
				$rndImgQry = "SELECT p.image AS image FROM product AS p JOIN product_to_category AS c ON p.product_id = c.product_id "
						. "WHERE c.category_id = " . $category_info['category_id'] . " AND p.image NOT LIKE 'no_image.jpg'";
				$rndImgRslt = mysql_query($rndImgQry) or die (mysql_error());
				if (mysql_num_rows($rndImgRslt) > 0)
				{
					$imgRows = array();
					while($imgRow = mysql_fetch_array($rndImgRslt))
					{
						$imgRows[] = $imgRow['image'];
					}
					$image = $imgRows[array_rand($imgRows)];
				}
			}
/catalog/controller/product/category.php
lines 109-113 before:

Code: Select all

if ($result['image']) {
						$image = $result['image'];
					} else {
						$image = 'no_image.jpg';
					}
after:

Code: Select all

if ($result['image']) {
						$image = $result['image'];
					} else {
						$image = 'no_image.jpg';
						// Use random image from products in the category
						$rndImgQry = "SELECT p.image AS image FROM product AS p JOIN product_to_category AS c ON p.product_id = c.product_id "
						       		. "WHERE c.category_id = " . $result['category_id'] . " AND p.image NOT LIKE 'no_image.jpg'";
						$rndImgRslt = mysql_query($rndImgQry) or die (mysql_error());
						if (mysql_num_rows($rndImgRslt) > 0)
						{
							$imgRows = array();
							while($imgRow = mysql_fetch_array($rndImgRslt))
							{
								$imgRows[] = $imgRow['image'];
							}
							$image = $imgRows[array_rand($imgRows)];
						}
					}
Would it be difficult to convert this code for version 2.3.0.2?

I tried but without success. My PHP knowledge is too low for this :-\

Newbie

Posts

Joined
Sat Apr 23, 2016 2:55 am

Post by straightlight » Sun May 03, 2020 8:08 pm

Lots of extensions that provides random products on the Marketplace: https://www.opencart.com/index.php?rout ... %20product .

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 OSWorX » Sun May 03, 2020 8:50 pm

straightlight wrote:
Sun May 03, 2020 8:08 pm
Lots of extensions that provides random products on the Marketplace: https://www.opencart.com/index.php?rout ... %20product .
Sorry, but after over 15 thousand posts, you should know how to read questions ..
This thread is not about random products, more to get a random image IF the category image is empty.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member
Online

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by straightlight » Sun May 03, 2020 8:52 pm

OSWorX wrote:
Sun May 03, 2020 8:50 pm
straightlight wrote:
Sun May 03, 2020 8:08 pm
Lots of extensions that provides random products on the Marketplace: https://www.opencart.com/index.php?rout ... %20product .
Sorry, but after over 15 thousand posts, you should know how to read questions ..
This thread is not about random products, more to get a random image IF the category image is empty.
Right ... I typed random products on the Marketplace. Apologize.

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 straightlight » Sun May 03, 2020 8:59 pm

As per the above:

/catalog/controller/product/category.php, find:

Code: Select all

if ($category_info['image']) {
				$data['thumb'] = $this->model_tool_image->resize($category_info['image'], $this->config->get($this->config->get('config_theme') . '_image_category_width'), $this->config->get($this->config->get('config_theme') . '_image_category_height'));
			} else {
				$data['thumb'] = '';
			}
			
after:

Code: Select all

if ($category_info['image']) {
				$image = $category_info['image'];
			} else {
				$image = '';
				// Use random image from products in the category
				$rndImgQry = "SELECT p.image AS image FROM " . DB_PREFIX . "product AS p JOIN " . DB_PREFIX . "product_to_category AS c ON p.product_id = c.product_id "
						. "WHERE c.category_id = " . $category_info['category_id'] . " AND p.image NOT LIKE 'no_image.jpg'";
						
				$rndImgRslt = $this->db->query($rndImgQry);
				
				if ($rndImgRslt->num_rows)
				{
					$imgRows = array();
					
					foreach ($rndImgRslt->rows as $imgRow)
					{
						$imgRows[] = html_entity_decode($imgRow['image'], ENT_QUOTES, 'UTF-8');
					}
					
					$image = $imgRows[array_rand($imgRows)];
				}
			}

/catalog/controller/product/category.php

Code: Select all

if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'], $this->config->get($this->config->get('config_theme') . '_image_product_width'), $this->config->get($this->config->get('config_theme') . '_image_product_height'));
				} else {
					$image = $this->model_tool_image->resize('placeholder.png', $this->config->get($this->config->get('config_theme') . '_image_product_width'), $this->config->get($this->config->get('config_theme') . '_image_product_height'));
				}
					

after:

Code: Select all

if ($result['image']) {
						$image = $result['image'];
					} else {
						$image = 'no_image.jpg';
						// Use random image from products in the category
						$rndImgQry = "SELECT p.image AS image FROM " . DB_PREFIX . "product AS p JOIN " . DB_PREFIX . "product_to_category AS c ON p.product_id = c.product_id "
						       		. "WHERE c.category_id = " . $result['category_id'] . " AND p.image NOT LIKE 'no_image.jpg'";
									
						$rndImgRslt = $this->db->query($rndImgQry);
						
						if ($rndImgRslt->num_rows)
						{
							$imgRows = array();
							
							foreach ($rndImgRslt->rows as $imgRow)
							{
								$imgRows[] = html_entity_decode($imgRow['image'], ENT_QUOTES, 'UTF-8');
							}
							
							$image = $imgRows[array_rand($imgRows)];
						}
					}

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 straightlight » Sun May 03, 2020 9:02 pm

Codes have been edited on my above post since the line numbers and the lookup lines are obviously different in more recent OC versions.

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 mnu59 » Tue May 05, 2020 10:08 pm

straightlight wrote:
Sun May 03, 2020 9:02 pm
Codes have been edited on my above post since the line numbers and the lookup lines are obviously different in more recent OC versions.
Thank you for your reply!

When I use the edited code, all the product images disappear in the category. Also there is still no category image.

Newbie

Posts

Joined
Sat Apr 23, 2016 2:55 am
Who is online

Users browsing this forum: No registered users and 94 guests