source: branches/feature-module-update/data/class/SC_CartSession.php @ 15177

Revision 15177, 10.9 KB checked in by nanasess, 17 years ago (diff)

slib.php のクラス化対応

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