【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)最後に、管理画面から、先に作っていた表示用ブロックを詳細ページまたは一覧ページのレイアウトに追加。

自サイトでアフィリエイトを行うプログラム

アフィリエイトといえば、A8やもしもが有名だけど、少しの商品・低予算でアフィリエイトを行いたい、アフィリエイトの形態を独自に決めたい、直接リンク元情報を得たいといった場合には、自サイトで独自にアフィリエイトを行いたい場合がある。

ASPは、統括管理ができたり、使い安い反面、費用や用途に制限があったりするが、自社でプログラムをインストールするなら、カスタマイズもできるし、管理情報も自社で持てる。また、長期利用してもコストの心配も要らない。

で、見つけたのがこれ。オンライン・デモもあるので先に試してみることができる。

Affiliate Pro。
※サイトを開くといきなり音が出て、ちょっとイラつくので注意(^^;

海外ソフトだが、管理画面も日本語翻訳されていて、機能も高価なASPに負けないくらい豊富。価格は、$49でお財布に優しいし、購入すれば、自分で運営している色々なサイトに使用できるので、お徳ではないでしょうか。色々見た中では、手頃でよさげ。

affiliatepro

こちらのサイトからなら更に10%OFF

その他のソフトを探すなら、こういうアフィリエイトやコマースソフトの一覧サイトもあります。http://www.topshareware.com/affiliate/downloads/1.htm

J. BTON ‘s Recent Posts mu-plugin for WordPress MU ver.2

[JAPANESE]

Download the code, and FTP-upload the file to /wp-ontent/mu-plugin/. It will work as soon as file uploaded.

**FEATURE**

Parameter explanations
$how_many: how many recent posts are being displayed
$how_long: time frame to choose recent posts from (in days)
$cate: category ID to be selected
$limit: how many posts should be displayed from each category
$titleOnly: true (only title of post is displayed) OR false (title of post and name of blog are displayed)
$begin_wrap: customise the start html code to adapt to different themes
$optmask: bitmask for various display options (default: 255)
 DISPLAY OPTIONS BITMASK
 1;  // gravatar
 2;  // date
 4;  // author name
 8;  // comment count
 16; // blog name
 32; // post name
 64; // post excerpt
 128; // excerpt capitalization
$exc_size: size of excerpt in words (default: 30)
$end_wrap: customise the end html code to adapt to different themes

Sample call:
1)You can edit the palameter in this plugin code L76,
   function ahp_recent_posts($how_many=9, $how_long=30, $cate=5, $limit=3, $titleOnly=false, $optmask=255, $exc_size=30, $begin_wrap='<li class=”pageposts_list”>’, $end_wrap='</li>’) {
and call the function in your template file like this…
   <ul><?php bton_recent_posts(); ?></ul>
2)If you want to call the parameter in your each code of the template,
change L75 –> delate ‘//’ from this code.
   function ahp_recent_posts($how_many, $how_long, $cate, $limit, $titleOnly, $optmask, $exc_size, $begin_wrap, $end_wrap) {
and coment out L76.
   //function ahp_recent_posts($how_many=9, $how_long=30, $cate=5, $limit=3, $titleOnly=false, $optmask=255, $exc_size=30, $begin_wrap='<li class=”pageposts_list”>’, $end_wrap='</li>’) {
So, you can set each parameter in your template
bton_recent_posts(9, 30, 5, 3, true, 255, 30, ‘<li class=”pageposts_list”>’, ‘</li>’); >> total 9 most recent entries over the past 30 days, from cateID 5, 3 entries from each blog, displaying titles only
 write code in template like this…
   <ul><?php bton_recent_posts(9, 30, 5, 3, true, 255, 30, ‘<li class=”pageposts_list”>’, ‘</li>’); ?></ul>
And you can also add style in your css files like this.
li.pageposts_list {list-mark-style:none;}
span.pageposts_cont{font-size:small; color:#333;}
span.pageposts_date {font-size:x-small; color:#ccc;}
span.pageposts_author {font-size:x-small; color:#ccc;}
span.pageposts_comment {font-size:x-small; color:#ccc;}
span.pageposts_blog {font-size:x-small; color:#ccc;}
span.pageposts_title {font-size:small; color:#333;}

->>[DOWNLOAD](bton_recent_posts_2.0.zip:4KB)

WORDPRESS MUで、各ブログの新着エントリーをカテゴリーごとに数件ずつ表示するMUプラグインver.2.0

[ENGLISH ]

速攻で失礼しますが、bton_recent_postsをバージョンアップします。

WORDPRESS MUで、マルチブログの各ブログについて(同じMU・DBで運用しているブログについて)、

  • 各サブブログから、
  • 指定したカテゴリーに属するエントリーで、
  • 公開されており、かつ、
  • エントリーの種類が「投稿=post」
  • エントリー後▲日以内の記事を
  • それぞれ○○件、最大●●件
  • アバター表示、コメント数と投稿者の表示対応(オプションマスクでそれぞれ表示・非表示を設定できる
  • XHTMLバリデート(※これについては特に書いていませんが、VER.1も同じです)

抽出して、表示させるMU-プラグイン。

こちらはver.2となっており、パラメータで色々設定できるのに対して、ver.1では、もう少しシンプルな設定です。完全にまとめてもいいのですが、カスタムフィールドなど他の要素を加えるときにマージできない場合があるかもしれないのと、そんなに複雑なものよりもう少しシンプルなほうがいい、という人もいると思いますので、両方のバージョン系列を分けました。

【概要】

サブブログ内のスパム・削除フラグなどが掛かっていない公開エントリーをメインブログのトップページなどに指定数だけ表示させる。

例えば、各サブブログにそれぞれNEWSカテゴリーがあり、NEWSカテゴリーの投稿を、サブブログごとに3件、3つのブログから合計9件、メインブログのトップページに表示させたい、といったときに使用する。

エントリーの抽出はカテゴリー単位で行うので、ブロック単位で、ニュースから2件ずつ、テーマから2件ずつ、という指定が可能。

bton_recent_posts()内のそれぞれの変数が表示の指定パラメータとなっている。(※ver.1より設定項目が増えていますのでご注意ください。)

bton_recent_posts($how_many, $how_long, $cate, $limit, $titleOnly, $optmask, $exc_size,$begin_wrap, $end_wrap)

  1. $how_many: 全体でいくつのエントリーを表示させるか
  2. $how_long: ▲日以内の投稿
  3. $cate: 抽出するカテゴリーのID
  4. $limit: 各ブログから抽出するそれぞれのエントリーの数
  5. $titleOnly: trueならタイトル、投稿日、出典ブログ名、内容の一部抜粋なども表示する /falseなら、タイトルとエントリーへのリンクのみ表示
  6. $optmask: 各項目の表示・非表示を設定する(デフォルト: 255)
     DISPLAY OPTIONS BITMASK
     1;  // gravatar(アバターアイコン)
     2;  // date(日付)
     4;  // author name(投稿者名)
     8;  // comment count(コメント数)
     16; // blog name (出典ブログ名)
     32; // post name (投稿のタイトル)
     64; // post excerpt(本文からの抽出文)
     128; // excerpt capitalization(英文で、抽出文の最初の数文字を大文字で表示する場合)
  7. $exc_size: size of excerpt in words (デフォルト: 30)
  8. $begin_wrap: リストを挟むHTMLタグ(開始タグ)
  9. $end_wrap: リストを挟むHTMLタグ(終了タグ)

表示の指定は、表示させたいテンプレート内(例えば、page.phpやindex.phpなど)で

<ul><?php bton_recent_posts(); ?></ul>

というように記述する。
それぞれの表示項目に対するオプションは、L76の対応する変数の値を編集する。

もし、このプラグインを使用して、条件の違うエントリー抽出を複数行いたい場合は、L75の「//」を削除してアクティブにし、L76行頭に「//」を追加して行をコメントアウトする。
そして、パラメータの指定は、<ul><?php bton_recent_posts(ここに順番に記述); ?></ul>内で指定すると良い。パターンを変えていくつも表示させられる。(例えば、トップページの左ナビでは、カテゴリーID5のニュースばかりを各ブログから3件ずつ表示させ、右のサイドバーには、カテゴリーID2のテーマのエントリーを1件ずつ表示させる、ということも可能。)共通のパラメータは、L75からL76にコピーして残せば、それについては、都度設定しなくとも良くなる。※パラメータ値の記述順番を間違わないように注意してください。

複数のカテゴリーを表示するときの参考コード例として、下記のようにカテゴリーIDを変えて、コードを呼び出せば、別のブロック位置であってもそれぞれエントリーが表示されるので、ポータル的な表示が可能。プラグインのカスタマイズで、色々とバリエーションが作れる。

プラグインのソース内では、
function bton_recent_posts($how_many, $how_long, $cate, $limit, $titleOnly=false, $optmask=255, $exc_size=30, $begin_wrap='<li class=”pageposts_list”>’, $end_wrap='</li>’) {

「タイトル以外の項目も表示、表示オプションの指定、抽出本文は30字、リストタグでまとめる」はプラグイン内で共通設定する。
<ul><?php bton_recent_posts(8, 30, 5, 2); ?></ul>//全部で8件、30日以内のエントリーをカテゴリー5から2件ずつ表示。
<ul><?php bton_recent_posts(4, 30, 1, 1); ?></ul>>//全部で4件、30日以内のエントリーをカテゴリー1から1件ずつ表示。
この2つを個別にテンプレートファイルに記述。

【使い方】
ソースをダウンロード・解凍し、FTPで、/wp_content/mu-plugin/ディレクトリにアップロード。(※mu-pluginディレクトリのプラグインは、管理画面のプラグインメニューで使用設定をしなくても、アップロードするだけで反映される。原則としてver.1と一緒には使えません。)

->>[ソースのダウンロード](bton_recent_posts_2.0.zip:4KB)

動作確認は一応していますが、ノーサポートです。もしものトラブル責任も負いません。ご自身のリスクの範囲内で自己責任で使用してください。