source: branches/comu-ver2/data/class/SC_CartSession.php @ 17970

Revision 17970, 12.2 KB checked in by Seasoft, 15 years ago (diff)

・商品送料対応

※商品規格ごとには設定できない難がある。

・送料カスタマイズ(拡張)を容易にするためのリファクタリング

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24/* カートセッション管理クラス */
25class SC_CartSession {
26    var $key;
27    var $key_tmp;   // ユニークIDを指定する。
28
29    /* コンストラクタ */
30    function SC_CartSession($key = 'cart') {
31        SC_Utils::sfDomainSessionStart();
32
33        if($key == "") $key = "cart";
34        $this->key = $key;
35    }
36
37    // 商品購入処理中のロック
38    function saveCurrentCart($key_tmp) {
39        $this->key_tmp = "savecart_" . $key_tmp;
40        // すでに情報がなければ現状のカート情報を記録しておく
41        if(count($_SESSION[$this->key_tmp]) == 0) {
42            $_SESSION[$this->key_tmp] = $_SESSION[$this->key];
43        }
44        // 1世代古いコピー情報は、削除しておく
45        foreach($_SESSION as $key => $val) {
46            if($key != $this->key_tmp && ereg("^savecart_", $key)) {
47                unset($_SESSION[$key]);
48            }
49        }
50    }
51
52    // 商品購入中の変更があったかをチェックする。
53    function getCancelPurchase() {
54        $ret = isset($_SESSION[$this->key]['cancel_purchase'])
55            ? $_SESSION[$this->key]['cancel_purchase'] : "";
56        $_SESSION[$this->key]['cancel_purchase'] = false;
57        return $ret;
58    }
59
60    // 購入処理中に商品に変更がなかったかを判定
61    function checkChangeCart() {
62        $change = false;
63        $max = $this->getMax();
64        for($i = 1; $i <= $max; $i++) {
65            if ($_SESSION[$this->key][$i]['quantity'] != $_SESSION[$this->key_tmp][$i]['quantity']) {
66                $change = true;
67                break;
68            }
69            if ($_SESSION[$this->key][$i]['id'] != $_SESSION[$this->key_tmp][$i]['id']) {
70                $change = true;
71                break;
72            }
73        }
74        if ($change) {
75            // 一時カートのクリア
76            unset($_SESSION[$this->key_tmp]);
77            $_SESSION[$this->key]['cancel_purchase'] = true;
78        } else {
79            $_SESSION[$this->key]['cancel_purchase'] = false;
80        }
81        return $_SESSION[$this->key]['cancel_purchase'];
82    }
83
84    // 次に割り当てるカートのIDを取得する
85    function getNextCartID() {
86        foreach($_SESSION[$this->key] as $key => $val){
87            $arrRet[] = $_SESSION[$this->key][$key]['cart_no'];
88        }
89        return (max($arrRet) + 1);
90    }
91
92    // 商品ごとの合計価格
93    function getProductTotal($arrInfo, $id) {
94        $max = $this->getMax();
95        for($i = 0; $i <= $max; $i++) {
96            if(isset($_SESSION[$this->key][$i]['id'])
97               && $_SESSION[$this->key][$i]['id'] == $id) {
98
99                // 税込み合計
100                $price = $_SESSION[$this->key][$i]['price'];
101                $quantity = $_SESSION[$this->key][$i]['quantity'];
102                $pre_tax = SC_Utils_Ex::sfPreTax($price, $arrInfo['tax'], $arrInfo['tax_rule']);
103                $total = $pre_tax * $quantity;
104                return $total;
105            }
106        }
107        return 0;
108    }
109
110    // 値のセット
111    function setProductValue($id, $key, $val) {
112        $max = $this->getMax();
113        for($i = 0; $i <= $max; $i++) {
114            if(isset($_SESSION[$this->key][$i]['id'])
115               && $_SESSION[$this->key][$i]['id'] == $id) {
116                $_SESSION[$this->key][$i][$key] = $val;
117            }
118        }
119    }
120
121    // カート内商品の最大要素番号を取得する。
122    function getMax() {
123        $cnt = 0;
124        $pos = 0;
125        $max = 0;
126        if (count($_SESSION[$this->key]) > 0){
127            foreach($_SESSION[$this->key] as $key => $val) {
128                if (is_numeric($key)) {
129                    if($max < $key) {
130                        $max = $key;
131                    }
132                }
133            }
134        }
135        return ($max);
136    }
137
138    // カート内商品数の合計
139    function getTotalQuantity() {
140        $total = 0;
141        $max = $this->getMax();
142        for($i = 0; $i <= $max; $i++) {
143            $total+= $_SESSION[$this->key][$i]['quantity'];
144        }
145        return $total;
146    }
147
148
149    // 全商品の合計価格
150    function getAllProductsTotal($arrInfo) {
151        // 税込み合計
152        $total = 0;
153        $max = $this->getMax();
154        for($i = 0; $i <= $max; $i++) {
155
156            if (!isset($_SESSION[$this->key][$i]['price'])) {
157                $_SESSION[$this->key][$i]['price'] = "";
158            }
159            $price = $_SESSION[$this->key][$i]['price'];
160
161            if (!isset($_SESSION[$this->key][$i]['quantity'])) {
162                $_SESSION[$this->key][$i]['quantity'] = "";
163            }
164            $quantity = $_SESSION[$this->key][$i]['quantity'];
165
166            $pre_tax = SC_Utils::sfPreTax($price, $arrInfo['tax'], $arrInfo['tax_rule']);
167            $total+= ($pre_tax * $quantity);
168        }
169        return $total;
170    }
171
172    // 全商品の合計税金
173    function getAllProductsTax($arrInfo) {
174        // 税合計
175        $total = 0;
176        $max = $this->getMax();
177        for($i = 0; $i <= $max; $i++) {
178            $price = $_SESSION[$this->key][$i]['price'];
179            $quantity = $_SESSION[$this->key][$i]['quantity'];
180            $tax = SC_Utils_Ex::sfTax($price, $arrInfo['tax'], $arrInfo['tax_rule']);
181            $total+= ($tax * $quantity);
182        }
183        return $total;
184    }
185
186    // 全商品の合計ポイント
187    function getAllProductsPoint() {
188        // ポイント合計
189        $total = 0;
190        if (USE_POINT !== false) {
191            $max = $this->getMax();
192            for($i = 0; $i <= $max; $i++) {
193                $price = $_SESSION[$this->key][$i]['price'];
194                $quantity = $_SESSION[$this->key][$i]['quantity'];
195
196                if (!isset($_SESSION[$this->key][$i]['point_rate'])) {
197                    $_SESSION[$this->key][$i]['point_rate'] = "";
198                }
199                $point_rate = $_SESSION[$this->key][$i]['point_rate'];
200
201                if (!isset($_SESSION[$this->key][$i]['id'][0])) {
202                    $_SESSION[$this->key][$i]['id'][0] = "";
203                }
204                $id = $_SESSION[$this->key][$i]['id'][0];
205                $point = SC_Utils_Ex::sfPrePoint($price, $point_rate, POINT_RULE, $id);
206                $total+= ($point * $quantity);
207            }
208        }
209        return $total;
210    }
211
212    // カートへの商品追加
213    function addProduct($id, $quantity, $campaign_id = "") {
214        $find = false;
215        $max = $this->getMax();
216        for($i = 0; $i <= $max; $i++) {
217
218            if($_SESSION[$this->key][$i]['id'] == $id) {
219                $val = $_SESSION[$this->key][$i]['quantity'] + $quantity;
220                if(strlen($val) <= INT_LEN) {
221                    $_SESSION[$this->key][$i]['quantity']+= $quantity;
222                    if(!empty($campaign_id)){
223                        $_SESSION[$this->key][$i]['campaign_id'] = $campaign_id;
224                        $_SESSION[$this->key][$i]['is_campaign'] = true;
225                    }
226                }
227                $find = true;
228            }
229        }
230        if(!$find) {
231            $_SESSION[$this->key][$max+1]['id'] = $id;
232            $_SESSION[$this->key][$max+1]['quantity'] = $quantity;
233            $_SESSION[$this->key][$max+1]['cart_no'] = $this->getNextCartID();
234            if(!empty($campaign_id)){
235                $_SESSION[$this->key][$max+1]['campaign_id'] = $campaign_id;
236                $_SESSION[$this->key][$max+1]['is_campaign'] = true;
237            }
238        }
239    }
240
241    // 前頁のURLを記録しておく
242    function setPrevURL($url) {
243        // 前頁として記録しないページを指定する。
244        $arrExclude = array(
245            "detail_image.php",
246            "/shopping/"
247        );
248        $exclude = false;
249        // ページチェックを行う。
250        foreach($arrExclude as $val) {
251            if(ereg($val, $url)) {
252                $exclude = true;
253                break;
254            }
255        }
256        // 除外ページでない場合は、前頁として記録する。
257        if(!$exclude) {
258            $_SESSION[$this->key]['prev_url'] = $url;
259        }
260    }
261
262    // 前頁のURLを取得する
263    function getPrevURL() {
264        return isset($_SESSION[$this->key]['prev_url'])
265            ? $_SESSION[$this->key]['prev_url'] : "";
266    }
267
268    // キーが一致した商品の削除
269    function delProductKey($keyname, $val) {
270        $max = count($_SESSION[$this->key]);
271        for($i = 0; $i < $max; $i++) {
272            if($_SESSION[$this->key][$i][$keyname] == $val) {
273                unset($_SESSION[$this->key][$i]);
274            }
275        }
276    }
277
278    function setValue($key, $val) {
279        $_SESSION[$this->key][$key] = $val;
280    }
281
282    function getValue($key) {
283        return $_SESSION[$this->key][$key];
284    }
285
286    function getCartList() {
287        $max = $this->getMax();
288        $arrRet = array();
289        for($i = 0; $i <= $max; $i++) {
290            if(isset($_SESSION[$this->key][$i]['cart_no'])
291               && $_SESSION[$this->key][$i]['cart_no'] != "") {
292                $arrRet[] = $_SESSION[$this->key][$i];
293            }
294        }
295        return $arrRet;
296    }
297
298    // カート内にある商品IDを全て取得する
299    function getAllProductID() {
300        $max = $this->getMax();
301        for($i = 0; $i <= $max; $i++) {
302            if($_SESSION[$this->key][$i]['cart_no'] != "") {
303                $arrRet[] = $_SESSION[$this->key][$i]['id'][0];
304            }
305        }
306        return $arrRet;
307    }
308
309    function delAllProducts() {
310        $max = $this->getMax();
311        for($i = 0; $i <= $max; $i++) {
312            unset($_SESSION[$this->key][$i]);
313        }
314    }
315
316    // 商品の削除
317    function delProduct($cart_no) {
318        $max = $this->getMax();
319        for($i = 0; $i <= $max; $i++) {
320            if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) {
321                unset($_SESSION[$this->key][$i]);
322            }
323        }
324    }
325
326    // 数量の増加
327    function upQuantity($cart_no) {
328        $max = $this->getMax();
329        for($i = 0; $i <= $max; $i++) {
330            if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) {
331                if(strlen($_SESSION[$this->key][$i]['quantity'] + 1) <= INT_LEN) {
332                    $_SESSION[$this->key][$i]['quantity']++;
333                }
334            }
335        }
336    }
337
338    // 数量の減少
339    function downQuantity($cart_no) {
340        $max = $this->getMax();
341        for($i = 0; $i <= $max; $i++) {
342            if($_SESSION[$this->key][$i]['cart_no'] == $cart_no) {
343                if($_SESSION[$this->key][$i]['quantity'] > 1) {
344                    $_SESSION[$this->key][$i]['quantity']--;
345                }
346            }
347        }
348    }
349
350    // カートの中の売り切れチェック
351    function chkSoldOut($arrCartList, $is_mobile = false){
352        foreach($arrCartList as $key => $val){
353            if($val['quantity'] == 0){
354                // 売り切れ商品をカートから削除する
355                $this->delProduct($val['cart_no']);
356                SC_Utils_Ex::sfDispSiteError(SOLD_OUT, "", true, "", $is_mobile);
357            }
358        }
359    }
360
361    /**
362     * カートの中のキャンペーン商品のチェック
363     * @param integer $campaign_id キャンペーンID
364     * @return boolean True:キャンペーン商品有り False:キャンペーン商品無し
365     */
366    function chkCampaign($campaign_id){
367        $max = $this->getMax();
368        for($i = 0; $i <= $max; $i++) {
369            if($_SESSION[$this->key][$i]['is_campaign'] and $_SESSION[$this->key][$i]['campaign_id'] == $campaign_id) return true;
370        }
371
372        return false;
373    }
374
375}
376?>
Note: See TracBrowser for help on using the repository browser.