忍者ブログ
[PR]
×

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



2024/03/19 15:23 |
EC-CUBE 複数商品の一括登録その2
続き

/data/class/page/products/LC_Page_Products_Buy.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
 
    require_once(CLASS_PATH . 
        "pages/frontparts/bloc/LC_Page_FrontParts_Bloc.php");
 
class LC_Page_Products_Buy extends LC_Page_FrontParts_Bloc {
    
    //定数
      const ERROR_MESSAGE = '買い物籠への投入処理に失敗しました';
    
    //変数
      protected $error_flg=false;
    
    //初期化
    public function init() {
        parent::init();
        //テンプレは使用しません
        //DB接続クラス
        $this->objDb = new SC_Helper_DB_Ex();
    }
 
    //デストラクタ.
    public function destroy() {
        parent::destroy();
    }
    
    //メイン処理
    public function process() {
        
        //引数から、商品番号および件数を取得
            $item_array=$this->getRequest($_REQUEST);
            if(!$item_array){
                $this->printError();
                exit();
            }
        
        //商品をカートに投入
            $ret=$this->setOrder($item_array);
            if($this->error_flg){
                $this->printError();
                exit();
            }
        
        //買い物が成功
            $this->processSuceed();
            exit();
        
    }
      
    //---------------------------------------------------
    // 商品IDから商品リストを作成する
    protected function getRequest($request){
        //返り値
            $ret=array();
        //繰り返し
        foreach($request['product_id'] as $key=>$val){
        
            //IDや数量欄が空白であれば異常
                if(!$val || !$request['quantity'][$key]){continue;}
            //ID毎にリクエストを纏める
                $product=array();
                $product['id']      = $val;
                $product['quantity']= $request['quantity'][$key];
                $product['cat1']    = isset($request['cat1'][$key]) 
                    ? $request['cat1'][$key] : 0;
                $product['cat2']    = isset($request['cat2'][$key])
                    ? $request['cat2'][$key] : 0;
            //引数に異常値があれば買い物籠に投入不可
                $product_check=$this->checkProductError($product);
                if(!$product_check)){
                    $this->error_flg=true;
                    return false;
                }
            //籠に入れる用リスト
                $ret[$key]=$product;
        }
        //返却
            return $ret;
    }
    
    //---------------------------------------------------
    // 配列に入ってきた商品を買い物籠に突っ込む
    protected function setOrder($item_array){
        //引数
        if(!is_array($item_array)){return false;}
        
        //インスタンス
            $objCartSess = new SC_CartSession();
        
        //各商品でループ
        foreach($item_array as $key=>$item){
                        
            //変数整理
                $product_id   =$item['id'];
                $quantity     =$item['quantity'];
                $product_cat1 =$item['cat1'] ? $item['cat1'] : "0";
                $product_cat2 =$item['cat2'] ? $item['cat2'] : "0";
            //実行
                //エラーが出ても取得できないので困る
                $objCartSess->addProduct(
                    array($product_id, $product_cat1, $product_cat2),$quantity
                );
        }
        
        //買い物かごの集計更新
            $this->setOrder_updateCart();
        
        //返却
            return true;
    }
    
    /*
       setOrderのサブルーチン
        買い物かごの集計を更新する
        これを行わないと、合計金額などが更新されなかったりする
    */
    protected function setOrder_updateCart(){
        //中身はLC_Page_Cart::process()のコピペ  どうなってるのかはよくわからない
        
        //インスタンス?
            global $objCampaignSess;
            $objView = new SC_SiteView(false);
            $objCartSess = new SC_CartSession("", false);
            $objSiteSess = new SC_SiteSession();
            $objCampaignSess = new SC_CampaignSession();
            $objSiteInfo = $objView->objSiteInfo;
            $objCustomer = new SC_Customer();
            $db = new SC_Helper_DB_Ex();
            $arrInfo = $objSiteInfo->data;
        //実行?
            $db->sfTotalCart($this, $objCartSess, $arrInfo);
            $db->sfTotalConfirm($this->arrData, 
                $this, $objCartSess, $arrInfo, $objCustomer);
        //終了?
            return true;
    }
    
    //---------------------------------------------------
    /*
        買い物籠に投入失敗時、エラーを表示して終了
    */
    protected function peinrError() {
        SC_Utils_Ex::sfDispSiteError(
          FREE_ERROR_MSG,false,false,self::ERROR_MESSAGE);
         exit();
    }    
    //---------------------------------------------------
    /*
        買い物籠に投入成功時、カートにリダイレクト
    */
    protected function processSuceed() {
          $this->sendRedirect($this->getLocation(URL_CART_TOP));
            exit();
    }
     
    //---------------------------------------------------
    /*
        サブルーチン
        基本的にLC_Page_Products_Detail等のコピペ、のはずだったがそうでもなくなった
    */
    
    /*
        商品ID、数量、サブカテゴリ1、サブカテゴリ2から、不正値がないかチェック
        
        ここらへんは本来SC_Helper_DBあたりに入れるべき
        ていうか、SC_Helper_Product的なものを作るべき
    */
    protected function checkProductError($product){
        //商品IDが数値でなければ異常
            if(!SC_Utils_Ex::sfIsInt($product['id'])){
                return false;
            }
        
        //該当の商品が存在しなければ異常
            if(!$this->objDb->sfIsRecord("dtb_products", "product_id"
                , $product['id'] , "del_flg = 0 AND status = 1")
            ){
                return false;
            }
        
        //商品を取得できるかどうか確認
            $ret=$this->getProductInfo($product);
            if(is_array($ret) && isset($ret['error'])){
                return false;
            }
        
        //商品の在庫チェック
        //商品の購入量制限チェック
            //めんどいので略
            
        //チェックを通過
            return true;
    }
    
    /*
        商品ID、サブカテゴリ1、サブカテゴリ2から、商品情報を取得
    */
    protected function getProductInfo($product){
    
        //SC_Helper_DB::sfGetProductsClass()が引数をlistで受け取るので順番を設定
            $product_obj=array();
            $product_obj[]=$product['id'];
            if($product['cat1'] != 0){
                $product_obj[]= $product['cat1'];
            }
            if($product['cat2'] != 0){
                $product_obj[]= $product['cat2'];
            }
        
        //取得
            $ret=$this->objDb->sfGetProductsClass($product_obj);
            if(!$ret){
                return false;
            }
        //返却
            return $ret;
    }
    
//↓クラスのおわり
}

products/buy.php?product_id[]=1&cat1[]=1&cat2[]=6&quantity[]=1&product_id[]=1&cat1[]=2&cat2[]=5&quantity[]=2&product_id[]=1&cat1[]=2&cat2[]=4&quantity[]=3

などというリクエストを送信すると任意の商品をさくさく登録することができます。

実はエラー制御とかわりと適当ですが気にしない。
SC_FormParamの使い方がかなり意味不明だったので自力でやってます。
他にも各ヘルパなんかの使い方がよくわかってないので無駄っぽいことをやってる気がします。
メソッド名とかも適当に過ぎますし。

ですが、元のソースもせめてこの程度には分割してほしい。
switchの中に長大なロジックとかいい加減もう見たくないぞ。
まあ、もっと言うならFactoryMethodかなんか使えって話ですが、さすがにそこまでするのはめんどい。


EC-CUBEの記事
PR


2010/03/08 21:47 | Comments(0) | TrackBack() | PHP

トラックバック

トラックバックURL:

コメント

コメントを投稿する






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



<<==NOVEL PHILOSOMA== 05 | HOME | EC-CUBE 複数商品の一括登録その1>>
忍者ブログ[PR]