zencart最近瀏覽模塊

在詳情頁獲取商品ID,用$_SESSION記錄最近瀏覽的商品ID,並按時間由近到遠順序排列.

如果在詳情頁,則最近瀏覽模塊裏不顯示當前商品


includes/modules/pages/product_info/header_php

<?php
/**
 * product_info header_php.php
 *
 * @package page
 * @copyright Copyright 2003-2011 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: header_php.php 18697 2011-05-04 14:35:20Z wilt $
 */

// 設定最近瀏覽過的商品數量
define('RECENTLY_VIEWED_NUMS', 10); 
 
// This should be first line of the script:
$zco_notifier->notify('NOTIFY_HEADER_START_PRODUCT_INFO');

require (DIR_WS_MODULES . zen_get_module_directory('require_languages.php'));

// if specified product_id is disabled or doesn't exist, ensure that metatags and breadcrumbs don't share inappropriate information
$sql = "select count(*) as total
          from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
          where    p.products_status = '1'
          and      p.products_id = '" . (int)$_GET['products_id'] . "'
          and      pd.products_id = p.products_id
          and      pd.language_id = '" . (int)$_SESSION['languages_id'] . "'";
$res = $db->Execute($sql);
if ($res->fields['total'] < 1)
{
	unset($_GET['products_id']);
	unset($breadcrumb->_trail[sizeof($breadcrumb->_trail) - 1]['title']);
	$robotsNoIndex = true;
	header('HTTP/1.1 404 Not Found');
}

// ensure navigation snapshot in case must-be-logged-in-for-price is enabled
if (!$_SESSION['customer_id'])
{
	$_SESSION['navigation']->set_snapshot();
}

// This should be last line of the script:
$zco_notifier->notify('NOTIFY_HEADER_END_PRODUCT_INFO');
$reviews_query_raw = "SELECT r.reviews_id, left(rd.reviews_text, 300) as reviews_text, r.reviews_rating, r.date_added, r.customers_name,r.yes,r.no
FROM " . TABLE_REVIEWS ." r, " . TABLE_REVIEWS_DESCRIPTION . " rd
WHERE r.products_id = :productsID
AND r.reviews_id = rd.reviews_id
AND r.status = 1 AND rd.languages_id = :languagesID ". $review_status . "
ORDER BY r.reviews_id desc";
$reviews_query_raw = $db->bindVars($reviews_query_raw, ':productsID', $_GET['products_id'],'integer');
$reviews_query_raw = $db->bindVars($reviews_query_raw,':languagesID', $_SESSION['languages_id'], 'integer');
$reviews_split = new splitPageResults($reviews_query_raw, MAX_DISPLAY_NEW_REVIEWS);
$reviews = $db->Execute($reviews_split->sql_query);
$reviewsArray = array();
while (!$reviews->EOF) {
$reviewsArray[] = array('id'=>$reviews->fields['reviews_id'],
'customersName'=>$reviews->fields['customers_name'],
'dateAdded'=>$reviews->fields['date_added'],
'reviewsText'=>$reviews->fields['reviews_text'],
'yes'=>$reviews->fields['yes'],
'no'=>$reviews->fields['no'],
'reviewsRating'=>$reviews->fields['reviews_rating']);
$reviews->MoveNext();
}

/*****************bof recently viewed products**************************/
if (!isset($_SESSION['recently_viewed'])) $_SESSION['recently_viewed'] = array();

if (!in_array((int)$_GET['products_id'], $_SESSION['recently_viewed'])) {
	$_SESSION['recently_viewed'][] = (int)$_GET['products_id'];
} else {
	// 如果當前商品ID存在於最近瀏覽商品ID中,則在最近瀏覽商品ID中刪除這個商品ID,
	// 並將當前商品ID追加到最近瀏覽商品的ID中
	$found_key = array_search((int)$_GET['products_id'],  $_SESSION['recently_viewed']);
	if ($found_key !== null) {
		unset($_SESSION['recently_viewed'][$found_key]);
		$_SESSION['recently_viewed'][] =(int)$_GET['products_id'];
	}
}

krsort($_SESSION['recently_viewed']);

// 只取最近瀏覽過的指定個數商品
if (count($_SESSION['recently_viewed']) > RECENTLY_VIEWED_NUMS) {
	// 注意,是按單元順序而不是按鍵值,一旦超出指定個數就把數組最後一個單元踢出去,
	// 只留最近指定數量商品
	array_pop($_SESSION['recently_viewed']);
}
/*****************eof recently viewed products**************************/




新建一個文件

includes/modules/你的模板/recently_products.php

<?php
/**
 * recently_products.php
 *
 * @package modules
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}

                   
// 初始化變量
$recently_products_query = '';
$recently = '';
$recently_array = $_SESSION['recently_viewed'];

// 商品詳情頁的最近瀏覽裏不要顯示當前商品
if ($current_page_base == 'product_info') {
	array_shift($recently_array);
}

if (zen_not_null($recently_array)) {
	foreach ($recently_array as $val) {
	    $recently .= $val . ','; 
    }
}

$recently = rtrim($recently, ',');

if (zen_not_null($recently)) {
    $recently_products_query = "SELECT distinct p.products_id, p.products_image, pd.products_name, p.master_categories_id 
                               FROM " . TABLE_PRODUCTS ." p, ". TABLE_PRODUCTS_DESCRIPTION . " pd 
                               WHERE p.products_id = pd.products_id
                               AND pd.language_id = '" . (int)$_SESSION['languages_id'] . "' 
                               AND p.products_status = 1
							   AND p.products_id IN (". $recently .") order by field(p.products_id, ". $recently .")";
}

$nums_max = RECENTLY_VIEWED_NUMS;
if (!$nums_max || $nums_max > RECENTLY_VIEWED_NUMS) $nums_max = RECENTLY_VIEWED_NUMS;
if ($recently_products_query != '') $recently_products = $db->Execute($recently_products_query, $nums_max);

$row = 0;
$col = 0;
$list_box_contents = array();
$title = '';
// 產品每行列數
$columns = 4;

$num_products_count = ($recently_products_query == '') ? 0 : $recently_products->RecordCount();

if ($num_products_count > 0) {
    if ($num_products_count < $columns) {
        $col_width = round(100/$num_products_count, 2);
    } else {
        $col_width = round(100/$columns);
    }
    
    while (!$recently_products->EOF) {
        $products_price = zen_get_products_special_price2($recently_products->fields['products_id']);
    if (!isset($productsInCategory[$recently_products->fields['products_id']])) $productsInCategory[$recently_products->fields['products_id']] = zen_get_generated_category_path_rev($recently_products->fields['master_categories_id']);
		
        $list_box_contents[$row][$col] = array('params' => '',
        'text' => (($recently_products->fields['products_image'] == '' and PRODUCTS_IMAGE_NO_IMAGE_STATUS == 0) ? '' : '<dd><a rel="nofollow" href="' . zen_href_link(zen_get_info_page($recently_products->fields['products_id']), 'cPath=' . $productsInCategory[$recently_products->fields['products_id']] . '&products_id=' . $recently_products->fields['products_id']) . '">' . zen_image(DIR_WS_IMAGES . $recently_products->fields['products_image'], $recently_products->fields['products_name'], '170') . '</a></dd>') . '<dt><a href="' . zen_href_link(zen_get_info_page($recently_products->fields['products_id']), 'cPath=' . $productsInCategory[$recently_products->fields['products_id']] . '&products_id=' . $recently_products->fields['products_id']) . '">'.$recently_products->fields['products_name'].'</a>' . $products_price . '</dt>');        
    
        $col ++;
        if ($col > ($columns - 1)) {
            $col = 0;
            $row ++;
        }
        $recently_products->MoveNext();
    }
    
    $title = '<h2 class="centerBoxHeading"><span>Recently Viewed</span></h2>';
    $zc_show_recently_products = true;
}

主要是這兩個文件,其他的好搞

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章