Recently Viewed Products Infobox for OSCommerce

for: OSC 2 CSS.

The following code yields a new infobox that displays the last four items viewed. Adapted from this OSCommerce Contribution to fit OSC 2 CSS. Caveat: I have had mixed success with rendering product urls but you can see a successful demo at www.niora.com. View some products and they start showing up in a box in the right hand column. What is unique about this code is that does not use database queries to display the products.

Installation:

2) Add the following command to the left or right column where you want the box to appear.

require(DIR_WS_BOXES . 'recently_viewed.php');

1) create a new file, recently_viewed.php. Paste the following code into this file and upload the file to the includes/boxes folder.

<?php
// Lets set the number of history items to display
$display_count = 4;
$action = (isset($_GET&#91;'action'&#93;) ? $_GET&#91;'action'&#93; : '');
if ($action == 'clear_history') {
unset($_SESSION&#91;'last_product_views'&#93;);
}
// Build history string
$last_product_views = $_SESSION&#91;'last_product_views'&#93;;
if ($last_product_views != "") {
$visited_array = explode('|', $_SESSION&#91;'last_product_views'&#93;);
$output_count = 1; 
foreach ($visited_array as $visited_array_item) {
$visited_item = trim($visited_array_item);
if ( ($visited_item != "") && ($output_count <= $display_count) ) {
$item_pieces = explode("^", $visited_item);
$item_image = trim($item_pieces&#91;0&#93;);
$item_name = trim($item_pieces&#91;1&#93;);
$item_url = trim($item_pieces&#91;2&#93;);
$visited_output .= '';
$last_visited_thumb = tep_image(DIR_WS_IMAGES . $item_image, $item_name, '75', '75');
$visited_output .= '<div class="pl-image"><a href="http://' . $item_url . '">' . $last_visited_thumb . '</a></div><div class="pl-name"><a href="http://' . $item_url . '">' . $item_name.'</a><br></div>';
$visited_output .= '';
$output_count++;
}
}
}
// Now look at the current page
if (basename($_SERVER['SCRIPT_NAME']) == "product_info.php") {
// First remove the action=clear_history from the url
$current_product_path = str_replace('&action=clear_history', '', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
//$pieces = explode("?osCsid", $current_product_path);
//$current_product_path = $pieces[0];
$new_array_item = $product_info['products_image'] . '^' . $product_info['products_name']. '^' . $current_product_path;
// and add it to the $_SESSION variable
$last_product_views = $_SESSION['last_product_views'];
if ($last_product_views == "") {
$_SESSION['last_product_views'] = trim($new_array_item);
} else {
$items_array = explode('|', $_SESSION['last_product_views']); 
$count = 1;
$new_array_string = '';
foreach ($items_array as $array_item) {
if ($count <= $display_count) {
$array_item = trim($array_item);
if ($array_item != "") {
if ($array_item != $new_array_item) {
$new_array_string .= "|" . $array_item;
$count++;
} 
}
}
}
$_SESSION&#91;'last_product_views'&#93; = $new_array_item . '|' . $new_array_string;
}
}
// Display history string to browser
if ($last_product_views != "") {
$info_box_contents = array();
$info_box_contents&#91;&#93; = array('text' => '<hr><div class="align_left"><strong>Last Viewed</strong></div>');
new cssinfoBoxHeading($info_box_contents, 'true', 'true', tep_href_link($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], tep_get_all_get_params(array('action')) . 'action=clear_history'));

$info_box_contents = array();
$info_box_contents[] = array('text' => $visited_output);
// new plcontentBox($info_box_contents);
new cssinfoBox($info_box_contents);
} // end if ($last_product_views != "")
?>