Post by qfreddie » Thu Nov 22, 2007 12:26 am

Hi

I would like on the homepage, below newest products, a similar listing, but with random products from all over the shop, from all the categories. Anybody tried to insert somenthing like this? It would be great :)

Newbie

Posts

Joined
Wed Nov 14, 2007 2:50 am

Post by chambrot » Sun Dec 16, 2007 11:40 am

I am also looking for a way to place "random products" on my homepage instead of the "latest products". Has anyone got this to work, or can someone point me in the right direction on how to get this done? Thanks a bunch.  ;D

Newbie

Posts

Joined
Sun Dec 02, 2007 2:00 pm

Post by bruce » Thu Jan 10, 2008 8:29 pm

To select a "random" product, you only need to generate a random number in the range of the minimum and maximum value of actual product_id values in your product table.

In the default installation the minimum is 1 and maximum is 4 so for the sake of example...

Code: Select all

srand ((double) microtime( )*1000000);
$random_number = rand(1,4);
To get the data for the "random" product add the following to catalog\controller\home.php

Code: Select all

        $random_product_data = array();
        //  repeat as required
        $random_products = $database->getRows("select * from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where p.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' and p.product_id = '" . (int)$random_product_number . "'");
 
        foreach ($random_products as $random_product) {
            $random_product_data[] = array(
                'name'  => $random_product['name'],
                'href'  => $url->href('product', FALSE, array('product_id' => $random_product['product_id'])),
                'thumb' => $image->resize($random_product['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
                'price' => $currency->format($tax->calculate($random_product['price'], $random_product['tax_class_id']))
            );
        }
This will give you one random product. Do this for as many times as the number of random products that you want, making sure you ignore any duplicates.

To get them on the home page, you will need to add the following to catalog\controller\home.php

Code: Select all

		$view->set('random_products', $random_product_data);
and, modify content\home.tpl in each of the templates that you are using to process 'random_products' similarly to how it already processes 'products'

Hope this helps

Active Member

Posts

Joined
Wed Dec 12, 2007 2:26 pm

Post by gabibv » Sun Apr 27, 2008 10:09 pm

Hello i need some help to put random products in my web shop.Please can u explain how to modifie product tabel and where is it please.Thank you

Newbie

Posts

Joined
Tue Oct 09, 2007 4:50 am

Post by bruce » Mon Apr 28, 2008 8:49 am

You don't need to add random products or change the product table. The code below shows how to select products randomly from your existing catalog of products.

Active Member

Posts

Joined
Wed Dec 12, 2007 2:26 pm

Post by paitken » Mon Apr 28, 2008 6:04 pm

You should just be able to ORDER BY RAND() off of the query bruce provided.

select * from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where p.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' ORDER BY RAND() LIMIT 1

This may or may not work, you might also need to limit it to more than one.  So you might want to change the LIMIT 1 to LIMIT 5 for 5 products.

Newbie

Posts

Joined
Mon Apr 28, 2008 5:57 pm

Post by April » Mon Jul 28, 2008 7:13 am

In catalog/controller/home.php I changed:

Code: Select all

$results = $database->getRows("select * from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where p.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' order by p.date_added desc limit 6");
to:

Code: Select all

$results = $database->getRows("select * from product p left join product_description pd on (p.product_id = pd.product_id) left join image i on (p.image_id = i.image_id) where p.status = '1' and pd.language_id = '" . (int)$language->getId() . "' and p.date_available < now() and p.status = '1' order by RAND() desc limit 6");
It seems to work well...please let me know if there are any drawbacks :-\ from doing it this way?..Thanks in advance..~april

Newbie

Posts

Joined
Fri Jul 18, 2008 2:42 am

Post by bruce » Mon Jul 28, 2008 7:19 am

Hi April,

the query posted in this thread by paitken generates a correct result.

I tried it on a 2000 product catalog with limit 4 instead of limit 1 and got 4 random products in 0.064 seconds. My concerns about the performance of "order by rand()" are unfounded.

Use the paitken query.

cheers

Bruce

Active Member

Posts

Joined
Wed Dec 12, 2007 2:26 pm
Who is online

Users browsing this forum: No registered users and 3 guests