EC-CUBE:カゴに入れた直後にカート画面に飛ばない。アラートボックスの表示方法。

「カゴに入れた直後にカート画面に飛ばないようにする」に、さらに機能を追加した。
http://www.itoben.com/blog/366.html

【注意】
ここで利用している「jQuery Noty」は、EC-CUBEに入っている、/html/jsjquery-1.4.2.min.jsでは動きません。
最新版のjsjquery.min.js、あるいはそれに近いものをダウンロードして適用してください。
data/Smarty/templates/default/site_frame.tplの修正も必要です。
戯れ問答さんの、ページにjQuery Notyの使い方などが詳しく書かれています。

次の3つの中から、表示方法を選択できるようにする。

(1) 「カゴに商品が追加されました。」というアラートボックスが表示されるので、OKをクリックしてアラートを閉じる。

(2) カートに移動するか、ページにとどまるかを「OK」または「キャンセル」で選択する。

(3) 「カゴに商品が追加されました。」とメッセージが表示され、2秒後に自動的に閉じる。あるいは、クリックしても閉じる。

1 アラートボックスの表示方法を選択できるようにするためにパラメータ(mtb_constantsテーブル)に登録する。
——————————————-
id:CARTIN_ALERT
name:3
rank:1250(←適宜)
remarks:カゴに入れた時のアラート表示 1/通常の「OK」ボタン 2/カートに移動か、ページにとどまるかを選択 3/自動的に閉じる
——————————————-
★システム設定>パラメーター設定を開き、「この内容で登録する」をクリック。

2 自動でアラートを閉じられるように、jQuery Notyを利用する。
http://needim.github.com/noty/

(1)ダウンロードしたファイルのうち、下記の3つのファイルを使用する。
jquery.noty.js jquery.noty.css noty_theme_default.css

(2)下記フォルダを作成し、3つのファイルを置く。
■html/js/jquery.noty

3 ■data/Smarty/templates/default/site_frame.tpl 追加

<!--アラートボックス-->
<!--{if $smarty.const.CARTIN_ALERT == 3}-->
<!--{assign var=list value="`$smarty.const.ROOT_URLPATH`products/list.php"}-->
<!--{assign var=detail value="`$smarty.const.ROOT_URLPATH`products/detail.php"}-->
<!--{if $smarty.server.PHP_SELF==$list || $smarty.server.PHP_SELF==$detail}-->
<link media="screen" rel="stylesheet" href="<!--{$smarty.const.ROOT_URLPATH}-->js/jquery.noty/jquery.noty.css" />
<link media="screen" rel="stylesheet" href="<!--{$smarty.const.ROOT_URLPATH}-->js/jquery.noty/noty_theme_default.css" />
<script type="text/javascript" src="<!--{$smarty.const.ROOT_URLPATH}-->js/jquery.noty/jquery.noty.js"></script>
<!--{/if}-->
<!--{/if}-->

4 商品詳細ページ

(1)■data/class/pages/products/LC_Page_Products_Detail.php
 160行目あたりを変更 (http://www.itoben.com/blog/366.htmlと同じ)

SC_Response_Ex::sendRedirect(CART_URLPATH);
exit;

↓(変更)

//カゴに入れた直後にカート画面に飛ばない。携帯とスマートフォンには適用しない。
if(SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE || SC_Display_Ex::detectDevice() == DEVICE_TYPE_SMARTPHONE) {
SC_Response_Ex::sendRedirect(CART_URLPATH);
exit;
} else {
//セッションに商品を買った事を保持する
$_SESSION['cart_buy_now'] = TRUE;
//リンク元のURLにリダイレクトさせる
SC_Response_Ex::sendRedirect($_SERVER['HTTP_REFERER']);
exit;
}

 337行目あたりに追加 function action()内の一番最後 (http://www.itoben.com/blog/366.htmlと同じ)

//カートに入れた状態をセッションで一時的に保持。アサイン後初期化。携帯とスマートフォンには適用しない。
if(SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE || SC_Display_Ex::detectDevice() == DEVICE_TYPE_SMARTPHONE) {
} else {
if ($_SESSION['cart_buy_now'] === true) {
$_SESSION['cart_buy_now'] = '';
$this->CartBuyNow = true;
}
// カテゴリーIDをアサイン
$this->tpl_Category_Id = $_GET['category_id'];
}

(2)■/data/Smarty/templates/default/products/detail.tpl

 45行目あたりを変更 (http://www.itoben.com/blog/366.htmlと同じ)

<form name="form1" id="form1" method="post" action="?">
↓
<form name="form1" id="form1" method="post" action="?<!--{if $tpl_Category_Id != ''}-->&category_id=<!--{$tpl_Category_Id}--><!--{/if}-->">

 一番下 の後に (http://www.itoben.com/blog/366.htmlを変更)

<!--{if $smarty.const.CARTIN_ALERT == 1}-->
<!--{if $CartBuyNow == true}-->
<script type="text/javascript">
alert("カゴに商品が追加されました。");
</script>
<!--{/if}-->
<!--{/if}-->

<!--{if $smarty.const.CARTIN_ALERT == 2}-->
<!--{if $CartBuyNow == true}-->
<script type="text/javascript">
if (confirm('カゴに商品が追加されました。\n「カゴの中を見る」に移動→OK\nページにとどまる→キャンセル')==true){
location.href = "<!--{$smarty.const.CART_URLPATH}-->"; // example_confirm.html へジャンプ
}
</script>
<!--{/if}-->
<!--{/if}-->

<!--{if $smarty.const.CARTIN_ALERT == 3}-->
<!--{if $CartBuyNow == true}-->
<script type="text/javascript">
noty({text: 'カゴに商品が追加されました。',layout: 'center', timeout: '2000'});
</script>
<!--{/if}-->
<!--{/if}-->

5 商品一覧ページ

(1)■data/class/pages/products/LC_Page_Products_List.php

 197行目あたりを変更 (http://www.itoben.com/blog/366.htmlと同じ)

SC_Response_Ex::sendRedirect(CART_URLPATH);
exit;

↓(変更)

//カゴに入れた直後にカート画面に飛ばない。携帯とスマートフォンには適用しない。
if(SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE || SC_Display_Ex::detectDevice() == DEVICE_TYPE_SMARTPHONE) {
SC_Response_Ex::sendRedirect(CART_URLPATH);
exit;
} else {
//セッションに商品を買った事を保持する
$_SESSION['cart_buy_now'] = TRUE;
//リンク元のURLにリダイレクトさせる
SC_Response_Ex::sendRedirect($_SERVER['HTTP_REFERER']);
exit;
}

 245行目あたりに追加 function action()内の一番最後 (http://www.itoben.com/blog/366.htmlと同じ)

//カートに入れた状態をセッションで一時的に保持。アサイン後初期化。携帯とスマートフォンには適用しない。
if(SC_Display_Ex::detectDevice() == DEVICE_TYPE_MOBILE || SC_Display_Ex::detectDevice() == DEVICE_TYPE_SMARTPHONE) {
} else {
if ($_SESSION['cart_buy_now'] === true) {
$_SESSION['cart_buy_now'] = '';
$this->CartBuyNow = true;
}
}

(2)■/data/Smarty/templates/default/products/list.tpl

 一番下 の後に  (http://www.itoben.com/blog/366.htmlを変更)

<!--{if $smarty.const.CARTIN_ALERT == 1}-->
<!--{if $CartBuyNow == true}-->
<script type="text/javascript">
alert("カゴに商品が追加されました。");
</script>
<!--{/if}-->
<!--{/if}-->

<!--{if $smarty.const.CARTIN_ALERT == 2}-->
<!--{if $CartBuyNow == true}-->
<script type="text/javascript">
if (confirm('カゴに商品が追加されました。\n「カゴの中を見る」に移動→OK\nページにとどまる→キャンセル')==true){
location.href = "<!--{$smarty.const.CART_URLPATH}-->"; // example_confirm.html へジャンプ
}
</script>
<!--{/if}-->
<!--{/if}-->

<!--{if $smarty.const.CARTIN_ALERT == 3}-->
<!--{if $CartBuyNow == true}-->
<script type="text/javascript">
noty({text: 'カゴに商品が追加されました。',layout: 'center', timeout: '2000'});
</script>
<!--{/if}-->
<!--{/if}-->