わりによくあるカスタマイズだと思うのですが、本ブログでは取り上げてなかったようです。
今回は、商品の一部に会員限定商品を設定し、会員限定商品に関してはカートボタンの代わりに「この商品は会員限定です。」と表示する、
という仕様をカスタマイズで実現したいと思います。
1.まず会員限定商品のフラグを設定します。
商品属性の中でどれを使ってもいいのですが、商品ステータスを使うとまずまず楽チンそうです。
システム設定>マスターデータ管理 の、mtb_status で新規レコードを追加します。
2.商品ステータスは商品詳細ページ等に商品ステータスアイコンが付く仕様になっていますので、
ついでにステータスアイコンのパスを設定しておくと良いでしょう。
システム設定>マスターデータ管理 の、mtb_status_image で新規レコードを追加します。
mtb_status_image で新規レコードを設定した場合は、該当するアイコン用画像を作成し、サーバーに保存しておきます。
保存場所は、html/user_data/packages/default/img/icon です。
こんな感じでしょうか。(お好きに剥がして使ってください。)
3.会員限定にしたい商品を編集して、商品ステータスの「会員限定商品」にチェックを入れます。
チェックを入れた商品に「会員限定商品」アイコンが付いているか、フロント側の商品一覧などで確認してみてください。
ここまで問題なければ前準備完了です。
4.商品詳細ページはわりと簡単です。
会員限定商品かどうかフラグを立てたいので、今回はテンプレート側で、商品ステータスアイコン表示の記述と絡めてやりたいと思います。
data/Smarty/templates/default/products/detail.tpl 内の商品ステータス部分の記述を以下のような感じにします。
コードを見るとわかる通り、商品ステータスIDが6の場合に…と決め打ちしているので、適宜数値は変更してください。
(本当はこういうのは パラメーターで管理する方が良いと思う)
1 | <!--▼商品ステータス--> |
2 | <!--{assign var=ps value=$productStatus[$tpl_product_id]}--> |
3 | <!--{assign var=customer_product_flg value=0}--> |
4 | <!--{if count($ps) > 0}--> |
5 | < ul class = "status_icon clearfix" > |
6 | <!--{foreach from=$ps item=status}--> |
7 | < li > |
8 | < img src=" <!--{$TPL_URLPATH}--> <!--{$arrSTATUS_IMAGE[$status]}--> " width="60" height="17" alt=" <!--{$arrSTATUS[$status]}--> " id="icon <!--{$status}--> " /> |
9 | </ li > |
10 | <!--{if $status == 6}--> <!--{assign var=customer_product_flg value=1}--> <!--{/if}--> |
11 | <!--{/foreach}--> |
12 | </ ul > |
13 | <!--{/if}--> |
14 | <!--▲商品ステータス--> |
5.最後にカート表示の記述部分で条件分岐を組み込みます。
data/Smarty/templates/default/products/detail.tpl 内のカートボタン表示部分の記述を以下のような感じにします。
なお、data/class/pages/products/LC_Page_Products_Detail.php の actionメソッド内で、
あらかじめログイン判定ロジックが仕込まれていて、$this->tpl_login に会員ログインしているか否かのフラグが入っています。
ですので、会員ログインしているか否かは、$tpl_login をフラグにすることができます。
1 | <!--▼買い物かご--> |
2 |
3 | < div class = "cart_area clearfix" > |
4 | < input type = "hidden" name = "mode" value = "cart" /> |
5 | < input type = "hidden" name = "product_id" value=" <!--{$tpl_product_id}--> " /> |
6 | < input type = "hidden" name = "product_class_id" value=" <!--{$tpl_product_class_id}--> " id="product_class_id" /> |
7 | < input type = "hidden" name = "favorite_product_id" value = "" /> |
8 |
9 | <!--{if ($customer_product_flg == 1 && $tpl_login == true) || $customer_product_flg == 0}--> |
10 | <!--{if $tpl_stock_find}--> |
11 | <!--{if $tpl_classcat_find1}--> |
12 | < div class = "classlist" > |
13 | <!--▼規格1--> |
14 | < ul class = "clearfix" > |
15 | < li > <!--{$tpl_class_name1|h}--> :</ li > |
16 | < li > |
17 | < select name = "classcategory_id1" style=" <!--{$arrErr.classcategory_id1|sfGetErrorColor}--> "> |
18 | <!--{html_options options=$arrClassCat1 selected=$arrForm.classcategory_id1.value}--> |
19 | </ select > |
20 | <!--{if $arrErr.classcategory_id1 != ""}--> |
21 | < br />< span class = "attention" >※ <!--{$tpl_class_name1}--> を入力して下さい。</ span > |
22 | <!--{/if}--> |
23 | </ li > |
24 | </ ul > |
25 | <!--▲規格1--> |
26 | <!--{if $tpl_classcat_find2}--> |
27 | <!--▼規格2--> |
28 | < ul class = "clearfix" > |
29 | < li > <!--{$tpl_class_name2|h}--> :</ li > |
30 | < li > |
31 | < select name = "classcategory_id2" style=" <!--{$arrErr.classcategory_id2|sfGetErrorColor}--> "> |
32 | </ select > |
33 | <!--{if $arrErr.classcategory_id2 != ""}--> |
34 | < br />< span class = "attention" >※ <!--{$tpl_class_name2}--> を入力して下さい。</ span > |
35 | <!--{/if}--> |
36 | </ li > |
37 | </ ul > |
38 | <!--▲規格2--> |
39 | <!--{/if}--> |
40 | </ div > |
41 | <!--{/if}--> |
42 |
43 | <!--★数量★--> |
44 | < dl class = "quantity" > |
45 | < dt >数量:</ dt > |
46 | < dd >< input type = "text" class = "box60" name = "quantity" value=" <!--{$arrForm.quantity.value|default:1|h}--> " maxlength=" <!--{$smarty.const.INT_LEN}--> " style=" <!--{$arrErr.quantity|sfGetErrorColor}--> " /> |
47 | <!--{if $arrErr.quantity != ""}--> |
48 | < br />< span class = "attention" > <!--{$arrErr.quantity}--> </ span > |
49 | <!--{/if}--> |
50 | </ dd > |
51 | </ dl > |
52 |
53 | < div class = "cartin" > |
54 | < div class = "cartin_btn" > |
55 | < div id = "cartbtn_default" > |
56 | <!--★カゴに入れる★--> |
57 | < a href = "javascript:void(document.form1.submit())" onmouseover="chgImg(' <!--{$TPL_URLPATH}--> img/button/btn_cartin_on.jpg','cart');" onmouseout="chgImg(' <!--{$TPL_URLPATH}--> img/button/btn_cartin.jpg','cart');"> |
58 | < img src=" <!--{$TPL_URLPATH}--> img/button/btn_cartin.jpg" alt="カゴに入れる" name="cart" id="cart" /></ a > |
59 | </ div > |
60 | </ div > |
61 | </ div > |
62 | < div class = "attention" id = "cartbtn_dynamic" ></ div > |
63 | <!--{else}--> |
64 | < div class = "attention" >申し訳ございませんが、只今品切れ中です。</ div > |
65 | <!--{/if}--> |
66 | <!--{else}--> |
67 | < div class = "attention" >この商品は会員限定です。</ div > |
68 | <!--{/if}--> |
これで商品詳細ページはできたので、次回は商品一覧ページをやりたいと思います。
ピンバック: [EC-CUBE 2.12.2] 会員ログイン時のみ商品が購入できるようにしたい(商品一覧ページ編) | NAKWEB × EC-CUBE