2010年4月6日火曜日

メール設定の動的データ挿入部分をカスタマイズ

data/Smarty/templates/テンプレートディレクトリ/mail_templates 内のファイルを修正または追加する。

追加する場合は、order_mail.tpl を別名で保存して別 ID のメールとして登録すればOK。

  1. システム設定(EC-CUBE 2.3.0)の「マスターデータ管理」で mtb_mail_template を選択し、ID と項目名を設定する。
  2. 次に、mtb_mail_tpl_path を選択し、ID と作成したファイルのパスを設定する。


在庫が少なくなったら、メールで知らせてくれる機能

/data/class/pages/shopping/LC_Page_Shopping_Complete.php

// 在庫を減らす処理

function lfReduceStock($objQuery, $arrID, $quantity) {

    $where = "product_id = ? AND classcategory_id1 = ? AND classcategory_id2 = ?";


    $arrRet = $objQuery->select("stock, stock_unlimited", "dtb_products_class", $where, $arrID);

    

    // 売り切れエラー

    if(($arrRet[0]['stock_unlimited'] != '1' && $arrRet[0]['stock'] < $quantity) || $quantity == 0) {


        $objQuery->rollback();

        sfDispSiteError(SOLD_OUT, "", true);

    // 無制限の場合、在庫はNULL

    } elseif($arrRet[0]['stock_unlimited'] == '1') {

        $sqlval['stock'] = null;


        $objQuery->update("dtb_products_class", $sqlval, $where, $arrID);

    // 在庫を減らす

    } else {

        $sqlval['stock'] = ($arrRet[0]['stock'] - $quantity);


        if($sqlval['stock'] == "") {

            $sqlval['stock'] = '0';

        }        

        $objQuery->update("dtb_products_class", $sqlval, $where, $arrID);


    }

    

// 在庫数が0の場合はNULL

    if( ($sqlval['stock'] <= 5) && ($arrRet[0]['stock_unlimited'] != '1') ){

        // メール送信先


        $stock_mail = "zaiko@domain.com";

        // 件名

        $stock_sub = "商品在庫が少なくなりました。";

        // メッセージ

        $stock_msg = "商品ID:".$arrID[0]."(".$arrID[1].",".$arrID[2].")"."の商品が少なくなりました。";


        // メールを送信します。

        mb_send_mail($stock_mail, $stock_sub, $stock_msg);

    }

}

テンプレートの中に別のテンプレート

1)管理画面からブロック(tempA)を作る。 (※既存のテンプレブロックの場合は、これは不要)

2)読み込む側のテンプレのソースにインクルード用のコードを記述する。

<!–{assign var=tempA value=$smarty.const.USER_TEMPLATE_PATH|cat:”default/bloc/tempA.tpl“}–>

<!–{include file=$tempA}–>


重要!)先に変数にアサインしてから、インクルードすること。上記ソース参照。

下記でも可能。

<!–{include file=`$smarty.const.USER_TEMPLATE_PATH`default/bloc/tempA.tpl}–>

閲覧履歴


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

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

メールテンプレートの追加方法

/data/Smarty/templates/テンプレート名/mail_templates 内の適当なテンプレートをコピーしてファイル名を変更し内容を適宜変更。(xxx.tpl)

システム設定→マスタデータ管理→ドロップダ ウンメニューから mtb_mail_tpl_path を選択。
・ID と 値 を追加。(mail_templates/xxx.tpl)

ド ロップダウンメニューから mtb_mail_template を選択。
・ID と 値 を追加。