zencart最近浏览模块

该博客介绍了如何在zencart电商平台上创建最近浏览模块。通过在产品详情页面获取商品ID,并利用$_SESSION存储,按照时间顺序展示用户的浏览历史,同时确保当前查看的商品不在最近浏览列表中。
摘要由CSDN通过智能技术生成

在详情页获取商品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;
}

主要是这两个文件,其他的好搞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值