【EC-CUBE】最近チェックした商品の履歴を○○件表示させる

商品の詳細ページと一覧ページで最近チェックした商品の履歴を表示させる。

  1. 履歴はクッキーで(1ヶ月)保持する
  2. 最新の閲覧商品を(4件)表示させ、重複する閲覧履歴は表示しない
  3. 表示テンプレートはブロックで生成し、一覧ページにも詳細ページにも共通して使えるようにする

その1)表示用ブロックを作成する。

管理画面から新規ブロックを作成し、下記コードを保存。
(下記はオススメ商品ブロックに倣った2列表示のテンプレートコード。商品名、価格、説明適宜修正する。)

<!–▼閲覧履歴ここから–>
<!–{if $arrItemHistory}–>
<div id=”whoboughtarea” class=”clearFix”>
<h2><img src=”<!–{$TPL_DIR}–>img/products/title_history.jpg” width=”580″ height=”30″ alt=”閲覧履歴” /></h2>
<div class=”whoboughtblock”>
<!–{section name=cnt loop=$arrItemHistory}–>
<!–{if ($smarty.section.cnt.index % 2) == 0}–>
<!–{if $arrItemHistory[cnt].product_id}–>
<!– 左列 –>
<div class=”whoboughtleft”>
<!–{if $arrItemHistory[cnt].main_list_image != “”}–>
<!–{assign var=image_path value=”`$arrItemHistory[cnt].main_list_image`”}–>
<!–{else}–>
<!–{assign var=image_path value=”`$smarty.const.NO_IMAGE_DIR`”}–>
<!–{/if}–>
<a href=”<!–{$smarty.const.DETAIL_P_HTML}–><!–{$arrItemHistory[cnt].product_id}–>”>
<img src=”<!–{$smarty.const.SITE_URL}–>resize_image.php?image=<!–{$image_path|sfRmDupSlash}–>&width=65&height=65″ alt=”<!–{$arrItemHistory[cnt].name|escape}–>” />
</a>

<!–{assign var=price02_min value=`$arrItemHistory[cnt].price02_min`}–>
<!–{assign var=price02_max value=`$arrItemHistory[cnt].price02_max`}–>
<h3><a href=”<!–{$smarty.const.DETAIL_P_HTML}–><!–{$arrItemHistory[cnt].product_id}–>”><!–{$arrItemHistory[cnt].name|escape}–></a></h3>

<p>価格<span class=”mini”>(税込)</span>:<span class=”price”>
<!–{if $price02_min == $price02_max}–>
<!–{$price02_min|sfPreTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}–>
<!–{else}–>
<!–{$price02_min|sfPreTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}–>~<!–{$price02_max|sfPreTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}–>
<!–{/if}–>円</span></p>
<p class=”mini”><!–{$arrItemHistory[cnt].main_list_comment|escape|nl2br}–></p>
</div>
<!– /左列 –>
<!–{/if}–>
<!–{/if}–>

<!–{if ($smarty.section.cnt.index % 2) != 0}–>
<!–{* assign var=nextCnt value=$smarty.section.cnt.index+1 *}–>
<!–{if $arrItemHistory[cnt].product_id}–>
<!– 右列 –>
<div class=”whoboughtright”>
<a href=”<!–{$smarty.const.DETAIL_P_HTML}–><!–{$arrItemHistory[cnt].product_id}–>”>
<!–{if $arrItemHistory[cnt].main_list_image != “”}–>
<!–{assign var=image_path value=”`$arrItemHistory[cnt].main_list_image`”}–>
<!–{else}–>
<!–{assign var=image_path value=”`$smarty.const.NO_IMAGE_DIR`”}–>
<!–{/if}–>
<img src=”<!–{$smarty.const.SITE_URL}–>resize_image.php?image=<!–{$image_path|sfRmDupSlash}–>&width=65&height=65″ alt=”<!–{$arrItemHistory[cnt].name|escape}–>” />
</a>
<!–{assign var=price02_min value=`$arrItemHistory[cnt].price02_min`}–>
<!–{assign var=price02_max value=`$arrItemHistory[cnt].price02_max`}–>
<h3><a href=”<!–{$smarty.const.DETAIL_P_HTML}–><!–{$arrItemHistory[cnt].product_id}–>”><!–{$arrItemHistory[cnt].name|escape}–></a></h3>

<p>価格<span class=”mini”>(税込)</span>:<span class=”price”>

<!–{if $price02_min == $price02_max}–>
<!–{$price02_min|sfPreTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}–>
<!–{else}–>
<!–{$price02_min|sfPreTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}–>~<!–{$price02_max|sfPreTax:$arrSiteInfo.tax:$arrSiteInfo.tax_rule|number_format}–>
<!–{/if}–>円</span></p>
<p class=”mini”><!–{$arrItemHistory[cnt].main_list_comment|escape|nl2br}–></p>
</div>
<!– /右列 –>
<!–{/if}–>
<!–{/if}–>
<!–{/section}–>
</div>
</div>
<!–{/if}–>

その2)詳細ページ用のphpに、コードを2箇所追加する。

/data/class/paes/products/LC_Page_Products_Detail.php

L100あたり、

// レイアウトデザインを取得
$helper = new SC_Helper_PageLayout_Ex();
$helper->sfGetPageLayout($this, false, “products/detail.php”);

// パラメータ管理クラス
$this->objFormParam = new SC_FormParam();
// パラメータ情報の初期化
$this->lfInitParam();
// POST値の取得
$this->objFormParam->setParam($_POST);

// ファイル管理クラス
$this->objUpFile = new SC_UploadFile(IMAGE_TEMP_DIR, IMAGE_SAVE_DIR);
// ファイル情報の初期化
$this->lfInitFile();

// 商品閲覧履歴取得
$this->Get_ItemHistory();

// 管理ページからの確認の場合は、非公開の商品も表示する。
if(isset($_GET[‘admin’]) && $_GET[‘admin’] == ‘on’) {
SC_Utils_Ex::sfIsSuccess(new SC_Session());
$status = true;
$where = “del_flg = 0”;
} else {
$status = false;
$where = “del_flg = 0 AND status = 1”;
}

そして、コードの一番最後のほう、 function lfConvertParam()の後ろにも追加。

function lfConvertParam() {
if (!isset($this->arrForm[‘quantity’][‘value’])) $this->arrForm[‘quantity’][‘value’] = “”;
$value = $this->arrForm[‘quantity’][‘value’];
$this->arrForm[‘quantity’][‘value’] = htmlspecialchars($value, ENT_QUOTES, CHAR_CODE);
}
//商品詳細閲覧履歴取得、表示データ取得処理
function Get_ItemHistory() {
$cnt = 0;
// ページを再読み込み後に表示
if (isset($_COOKIE[‘product’])) {
foreach ($_COOKIE[‘product’] as $name => $value) {
$objQuery = new SC_Query();
// DBから一覧表示用商品情報取得
$arrRet = $objQuery->select(“*”, “vw_products_allclass AS alldtl”, “product_id =”.$value);
$this->arrItemHistory[$cnt] = $arrRet[0];
$cnt = $cnt+1;
}
}

//クッキーに重複項目がないか判定処理
$duplicateFlg = true;
if (isset($_COOKIE[‘product’])) {
foreach ($_COOKIE[‘product’] as $name => $value) {
if($value == $_GET[‘product_id’]){
$duplicateFlg = false;
}
}
}

//重複がない場合クッキーに設定
if($duplicateFlg){
if($cnt < 4){
$cnt = $cnt + 1;
setcookie(“product[“.$cnt.”]”, $_GET[‘product_id’],time()+60*60*24*30 );
}else{
$reNum = 1;
foreach ($_COOKIE[‘product’] as $name => $value) {
if($reNum > 1){
$setNum = $reNum -1;
setcookie(“product[“.$setNum.”]”, $value, time()+60*60*24*30 );
}
$reNum = $reNum + 1;
}
setcookie(“product[4]”, $_GET[‘product_id’], time()+60*60*24*30 );
}
}
}
}
?>

その3)一覧ページ用phpにも同様にコードを追加する。

/data/class/paes/products/LC_Page_Products_list.php

L82あたり、

// レイアウトデザインを取得
$helper = new SC_Helper_PageLayout_Ex();
$helper->sfGetPageLayout($this, false, DEF_LAYOUT);

//2008.04.11 商品閲覧履歴取得
$this->Get_ItemHistory();

// 管理ページからの確認の場合は、非公開の商品も表示する。
if(isset($_GET[‘admin’]) && $_GET[‘admin’] == ‘on’) {
SC_Utils_Ex::sfIsSuccess(new SC_Session());
$status = true;
$where = “del_flg = 0”;
} else {
$status = false;
$where = “del_flg = 0 AND status = 1”;
}

//表示件数の選択
if(isset($_POST[‘disp_number’])
&& SC_Utils_Ex::sfIsInt($_POST[‘disp_number’])) {
$this->disp_number = $_POST[‘disp_number’];
} else {

コードの最後のほう、

function lfconvertParam () {
foreach ($this->arrForm as $key => $value) {
if (preg_match(‘/^quantity[0-9]+/’, $key)) {
$this->arrForm[$key]
= htmlspecialchars($this->arrForm[$key], ENT_QUOTES, CHAR_CODE);
}
}
}
//2008.04.11 商品詳細閲覧履歴取得、表示データ取得処理
function Get_ItemHistory() {
$cnt = 0;
// ページを再読み込み後に表示
if (isset($_COOKIE[‘product’])) {
foreach ($_COOKIE[‘product’] as $name => $value) {
$objQuery = new SC_Query();
// DBから一覧表示用商品情報取得
$arrRet = $objQuery->select(“*”, “vw_products_allclass AS alldtl”, “product_id =”.$value);
$this->arrItemHistory[$cnt] = $arrRet[0];
$cnt = $cnt+1;
}
}

//クッキーに重複項目がないか判定処理
$duplicateFlg = true;
foreach ($_COOKIE[‘product’] as $name => $value) {
if($value == $_GET[‘product_id’]){
$duplicateFlg = false;
}
}

//重複がない場合クッキーに設定
if($duplicateFlg){
if($cnt < 4){
$cnt = $cnt + 1;
setcookie(“product[“.$cnt.”]”, $_GET[‘product_id’],time()+60*60*24*30 );
}else{
$reNum = 1;
foreach ($_COOKIE[‘product’] as $name => $value) {
if($reNum > 1){
$setNum = $reNum -1;
setcookie(“product[“.$setNum.”]”, $value, time()+60*60*24*30 );
}
$reNum = $reNum + 1;
}
setcookie(“product[4]”, $_GET[‘product_id’], time()+60*60*24*30 );
}
}
}
}
?>

を追加。

その4)最後に、管理画面から、先に作っていた表示用ブロックを詳細ページまたは一覧ページのレイアウトに追加。