忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/04/26 17:48 |
EC-CUBE 購入履歴をブロック化

前回の記事を参考に、現在マイページに固定で書かれており流用ができない購入履歴をブロックに切り出してみます。

まず管理画面からデザイン管理→新規ブロックを作成。
ファイル名はorder_history。
中身はとりあえず空白でかまいません。

データベースにブロックが登録され、
/htdocs/user_data/packages/default/bloc/order_history.tpl
にテンプレートファイルが作成されます。

データベースを編集。
dtb_bloc.php_pathfrontparts/bloc/order_history.phpと記入します。

/html/frontparts/bloc/order_history.phpを作成。
他のファイルをコピペして、
 

1
2
3
    require_once(CLASS_EX_PATH . 
       "page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_OrderHistory_Ex.php");
    $objPage = new LC_Page_FrontParts_Bloc_OrderHistory_Ex();

の2行を修正するだけです。

data/class_extends/page_extends/frontparts/bloc/LC_Page_FrontParts_Bloc_OrderHistory_Ex.phpを作成。
これまた同様に他のファイルをコピペし、
 

1
2
3
4
    require_once(CLASS_PATH . 
        "pages/frontparts/bloc/LC_Page_FrontParts_Bloc_OrderHistory.php");
    class LC_Page_FrontParts_Bloc_OrderHistory_Ex 
        extends LC_Page_FrontParts_Bloc_OrderHistory {


の2行を修正するだけです。

/data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_OrderHistory.phpを作成。
ここはロジックを書かないといけないのでコピペでは終われません。
購入履歴を取得するロジックはマイページを作成する部分、/data/class/pages/mypage/LC_Page_Mypage.phpLC_Page_MyPage::process()に書かれていますので、その部分をまるごとコピペします。
同じコードを複数の箇所にコピペというのは本来やってはいけない開発手法なのですが、EC-CUBE上での解決方法がわかりませんでした。

全体を表示するわけではなくブロックを表示するだけなので、全体レイアウトを定義するSC_Helper_PageLayout_Exあたりを呼ぶ必要はありません。
またブロックなのでページングまでは要らないや、マイページに飛ばせばいいかということでページング部分も削除。

/data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_OrderHistory.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require_once(CLASS_PATH . "pages/frontparts/bloc/LC_Page_FrontParts_Bloc.php");
 
class LC_Page_FrontParts_Bloc_OrderHistory extends LC_Page_FrontParts_Bloc {
    function init() {
        parent::init();
        $bloc_file = 'order_history.tpl';
        $this->setTplMainpage($bloc_file);
    }
 
    function process() {
    
        $objView = new SC_SiteView();
        $objQuery = new SC_Query();
        $objCustomer = new SC_Customer();
        
        // ログインチェック
        if(!$objCustomer->isLoginSuccess()) {
            SC_Utils_Ex::sfDispSiteError(CUSTOMER_ERROR);
        }else {
            //個人情報
            $this->CustomerName1 = $objCustomer->getvalue('name01');
            $this->CustomerName2 = $objCustomer->getvalue('name02');
        }
        
        //SQL
        $col = "order_id, create_date, payment_id, payment_total";
        $from = "dtb_order";
        $where = "del_flg = 0 AND customer_id=?";
        $arrval = array($objCustomer->getvalue('customer_id'));
        $order = "order_id DESC";
        
        //全件数
        $linemax = $objQuery->count($from, $where, $arrval);
        $this->tpl_linemax = $linemax;
 
        // 取得範囲の指定(開始行番号、行数のセット)
        $objQuery->setlimitoffset(SEARCH_PMAX, 0);
        // 表示順序
        $objQuery->setorder($order);
 
        //購入履歴の取得
        $this->arrOrder = $objQuery->select($col, $from, $where, $arrval);
 
        // 支払い方法の取得
        $objDb = new SC_Helper_DB_Ex();
        $this->arrPayment = $objDb->sfGetIDValueList(
       "dtb_payment", "payment_id", "payment_method");
        
        //テンプレ
        $objView->assignobj($this);
        $objView->display($this->tpl_mainpage);
    }
 
    function destroy() {   parent::destroy();   }}


生SQLとかモデルに隠せよ……

最後はテンプレートを編集します。
/htdocs/members/user_data/packages/default/bloc/order_history.tplを直接、もしくは管理画面のブロック管理からでも編集できます。
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<div id="order_history">
  <!--{if $tpl_linemax > 0}-->
    <form name="form1" method="post" 
       action="<!--{$smarty.const.URL_DIR}-->mypage/index.php">
        <input type="hidden" name="order_id" value="" />
        <p>
            <!--{$CustomerName1|escape}--> <!--{$CustomerName2|escape}-->
            様のご注文履歴。
         </p>
        <p>
            最新<!--{$smarty.const.SEARCH_PMAX|escape}-->件を表示しています。
            <a href="<!--{$smarty.const.URL_DIR}-->mypage/">
            全ての履歴を確認</a>
        </p>
        <table summary="購入履歴">
          <tr>
            <th>購入日時</th>
            <th>注文番号</th>
            <th>お支払い方法</th>
            <th>合計金額</th>
            <th>詳細</th>
          </tr>
          <!--{section name=cnt loop=$arrOrder}-->
            <tr>
              <td><!--{$arrOrder[cnt].create_date|sfDispDBDate}--></td>
              <td><!--{$arrOrder[cnt].order_id}--></td>
              <!--{assign var=payment_id value="`$arrOrder[cnt].payment_id`"}-->
              <td><!--{$arrPayment[$payment_id]|escape}--></td>
              <td class="pricetd">
                  <!--{$arrOrder[cnt].payment_total|number_format}--></td>
              <td class="centertd">
                <a href="<!--{$smarty.server.PHP_SELF|escape}-->" 
                 onclick="fnChangeAction('
                    <!--{$smarty.const.URL_DIR}-->mypage/history.php');
                    fnKeySubmit('order_id','<!--{$arrOrder[cnt].order_id}-->');
                    return false">詳細</a>
              </td>
            </tr>
          <!--{/section}-->
        </table>
      </form>
    <!--{/if}-->
</div>


ここもマイページの購入履歴表示部分をそっくりコピペ。
submitのないフォームになんの意味があるかというと、詳細リンク押下を探知して送信内容を書き換えてPOST送信するというかなり意味不明なことを行っています。
素直に<a href="<!--{$smarty.const.URL_DIR}-->mypage/history.php?order_id=<!--{$arrOrder[cnt].order_id}-->">ってやって$_REQUESTで受け取れば平穏無事に穏便に終わるのに何故POSTでしか受け取れないような作りになってるんだろう。
注文番号は連番なのでリファラ隠したところで全く意味ないですし。

以上で、好きなページに購入履歴ブロックを追加することができるようになりました。
めでたし。


EC-CUBEの記事

PR


2010/02/19 21:42 | Comments(0) | TrackBack() | PHP

トラックバック

トラックバックURL:

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<Smartyで文字コードの違うPHPをincludeする | HOME | ==NOVEL PHILOSOMA== 02>>
忍者ブログ[PR]