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

Revision 15532, 11.7 KB checked in by nanasess, 17 years ago (diff)

svn:mime-type 修正

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