Randomize related items (v. 2.8x)
This modification shows a random set of items in the same category as the selected item (based on the selected item's first category), if the client hasn't specified related items.
In other words, if a customer selects a candle that has 'Candles' as its first category, five other items that have 'Candles' as any category will show as the related items. (If the client has specified related items, though, they will be shown instead of these random items.)
-
Open the products_detail.php, products_multisingle.php and products_single.php files in the /enc/ folder.
-
Find the line that reads
$relnum = mysql_num_rows($relresult);
-
Just under that line, add the code
// NO RELATED ITEMS - USE RANDOM
if ($relnum == 0)
{
$relquery = "SELECT ID, Item, SmImage, Keywords FROM " .SB_DB_PREFIX ."_items WHERE Active='Yes' AND ID<>'$item' ";
if(!empty($searchrow[Category1]))
$relquery .= "AND Category" .implode("='" .(int)$searchrow[Category1] ."' OR Category", range(1,15)) ."='" .(int)$searchrow[Category1] ."' ";
if ($Show_OOS == "No")
$relquery .= " AND OutOfStock='No' AND (Inventory > 0 OR Inventory IS NULL) ";
$relquery .= "ORDER BY rand() LIMIT 5";
$relresult = mysql_query($relquery, $dblink) or die ("Unable to view related items.");
$relnum = mysql_num_rows($relresult);
} -
Optional: if you want to show the newest items entered instead of just random items, change the above line:
$relquery .= "ORDER BY rand() LIMIT 5";
to:
$relquery .= "ORDER BY DateEdited DESC LIMIT 5";
-
Optional: if you want to show more or less than 5 items, change the 5 after 'LIMIT' to the number of items you want to display.
-
Save and reload all files.