source: branches/version-2_5-dev/data/class/SC_CartSession.php @ 18852

Revision 18852, 16.2 KB checked in by nanasess, 14 years ago (diff)

商品種別によってカートを分ける(#823)

  • SC_CartSession をリファクタリング
  • 商品情報を SC_CartSession で取得するように変更
  • SC_Helper_DB::sfTotalCart() の実装を SC_CartSession へ移動
  • SC_Product で, 規格分類名を name に格納していたのを classcategory_name へ変更
  • カート関連の処理を修正
  • 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-2010 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    /** ユニークIDを指定する. */
27    var $key_tmp;
28
29    /** カートのセッション変数. */
30    var $cartSession;
31
32    /* コンストラクタ */
33    function SC_CartSession($cartKey = "cart") {
34        $this->cartSession =& $_SESSION[$cartKey];
35    }
36
37    // 商品購入処理中のロック
38    function saveCurrentCart($key_tmp, $productTypeId) {
39        $this->key_tmp = "savecart_" . $key_tmp;
40        // すでに情報がなければ現状のカート情報を記録しておく
41        if(count($_SESSION[$this->key_tmp]) == 0) {
42            $_SESSION[$this->key_tmp] = $this->cartSession[$productTypeId];
43        }
44        // 1世代古いコピー情報は、削除しておく
45        foreach($_SESSION as $k => $val) {
46            if($k != $this->key_tmp && preg_match("/^savecart_/", $k)) {
47                unset($this->cartSession[$productTypeId][$k]);
48            }
49        }
50        $this->registerKey($productTypeId);
51    }
52
53    // 商品購入中の変更があったかをチェックする。
54    function getCancelPurchase($productTypeId) {
55        $ret = isset($this->cartSession[$productTypeId]['cancel_purchase'])
56            ? $this->cartSession[$productTypeId]['cancel_purchase'] : "";
57        $this->cartSession[$productTypeId]['cancel_purchase'] = false;
58        return $ret;
59    }
60
61    // 購入処理中に商品に変更がなかったかを判定
62    function checkChangeCart($productTypeId) {
63        $change = false;
64        $max = $this->getMax($productTypeId);
65        for($i = 1; $i <= $max; $i++) {
66            if ($this->cartSession[$productTypeId][$i]['quantity']
67                != $_SESSION[$this->key_tmp][$i]['quantity']) {
68
69                $change = true;
70                break;
71            }
72            if ($this->cartSession[$productTypeId][$i]['id']
73                != $_SESSION[$this->key_tmp][$i]['id']) {
74
75                $change = true;
76                break;
77            }
78        }
79        if ($change) {
80            // 一時カートのクリア
81            unset($_SESSION[$this->key_tmp]);
82            $this->cartSession[$productTypeId][$key]['cancel_purchase'] = true;
83        } else {
84            $this->cartSession[$productTypeId]['cancel_purchase'] = false;
85        }
86        return $this->cartSession[$productTypeId]['cancel_purchase'];
87    }
88
89    // 次に割り当てるカートのIDを取得する
90    function getNextCartID($productTypeId) {
91        foreach($this->cartSession[$productTypeId] as $key => $val){
92            $arrRet[] = $this->cartSession[$productTypeId][$key]['cart_no'];
93        }
94        return (max($arrRet) + 1);
95    }
96
97    /**
98     * 商品ごとの合計価格
99     * XXX 実際には、「商品」ではなく、「カートの明細行(≒商品規格)」のような気がします。
100     *
101     * @param integer $id
102     * @return string 商品ごとの合計価格(税込み)
103     * @deprecated SC_CartSession::getCartList() を使用してください
104     */
105    function getProductTotal($id, $productTypeId) {
106        $max = $this->getMax($productTypeId);
107        for($i = 0; $i <= $max; $i++) {
108            if(isset($this->cartSession[$productTypeId][$i]['id'])
109               && $this->cartSession[$productTypeId][$i]['id'] == $id) {
110
111                // 税込み合計
112                $price = $this->cartSession[$productTypeId][$i]['price'];
113                $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
114                $pre_tax = SC_Helper_DB_Ex::sfPreTax($price);
115                $total = $pre_tax * $quantity;
116                return $total;
117            }
118        }
119        return 0;
120    }
121
122    // 値のセット
123    function setProductValue($id, $key, $val, $productTypeId) {
124        $max = $this->getMax($productTypeId);
125        for($i = 0; $i <= $max; $i++) {
126            if(isset($this->cartSession[$productTypeId][$i]['id'])
127               && $this->cartSession[$productTypeId][$i]['id'] == $id) {
128                $this->cartSession[$productTypeId][$i][$key] = $val;
129            }
130        }
131    }
132
133    // カート内商品の最大要素番号を取得する。
134    function getMax($productTypeId) {
135        $max = 0;
136        if (count($this->cartSession[$productTypeId]) > 0){
137            foreach($this->cartSession[$productTypeId] as $key => $val) {
138                if (is_numeric($key)) {
139                    if($max < $key) {
140                        $max = $key;
141                    }
142                }
143            }
144        }
145        return ($max);
146    }
147
148    // カート内商品数の合計
149    function getTotalQuantity($productTypeId) {
150        $total = 0;
151        $max = $this->getMax($productTypeId);
152        for($i = 0; $i <= $max; $i++) {
153            $total+= $this->cartSession[$productTypeId][$i]['quantity'];
154        }
155        return $total;
156    }
157
158
159    // 全商品の合計価格
160    function getAllProductsTotal($productTypeId) {
161        // 税込み合計
162        $total = 0;
163        $max = $this->getMax($productTypeId);
164        for($i = 0; $i <= $max; $i++) {
165
166            if (!isset($this->cartSession[$productTypeId][$i]['price'])) {
167                $this->cartSession[$productTypeId][$i]['price'] = "";
168            }
169
170            $price = $this->cartSession[$productTypeId][$i]['price'];
171
172            if (!isset($this->cartSession[$productTypeId][$i]['quantity'])) {
173                $this->cartSession[$productTypeId][$i]['quantity'] = "";
174            }
175            $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
176
177            $pre_tax = SC_Helper_DB_Ex::sfPreTax($price);
178            $total+= ($pre_tax * $quantity);
179        }
180        return $total;
181    }
182
183    // 全商品の合計税金
184    function getAllProductsTax($productTypeId) {
185        // 税合計
186        $total = 0;
187        $max = $this->getMax($productTypeId);
188        for($i = 0; $i <= $max; $i++) {
189            $price = $this->cartSession[$productTypeId][$i]['price'];
190            $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
191            $tax = SC_Helper_DB_Ex::sfTax($price);
192            $total+= ($tax * $quantity);
193        }
194        return $total;
195    }
196
197    // 全商品の合計ポイント
198    function getAllProductsPoint($productTypeId) {
199        // ポイント合計
200        $total = 0;
201        if (USE_POINT !== false) {
202            $max = $this->getMax($productTypeId);
203            for($i = 0; $i <= $max; $i++) {
204                $price = $this->cartSession[$productTypeId][$i]['price'];
205                $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
206
207                if (!isset($this->cartSession[$productTypeId][$i]['point_rate'])) {
208                    $this->cartSession[$productTypeId][$i]['point_rate'] = "";
209                }
210                $point_rate = $this->cartSession[$productTypeId][$i]['point_rate'];
211
212                if (!isset($this->cartSession[$productTypeId][$i]['id'][0])) {
213                    $this->cartSession[$productTypeId][$i]['id'][0] = "";
214                }
215                $id = $this->cartSession[$productTypeId][$i]['id'][0];
216                $point = SC_Utils_Ex::sfPrePoint($price, $point_rate, POINT_RULE, $id);
217                $total+= ($point * $quantity);
218            }
219        }
220        return $total;
221    }
222
223    // カートへの商品追加
224    function addProduct($id, $quantity, $productTypeId) {
225        $find = false;
226        $max = $this->getMax($productTypeId);
227        for($i = 0; $i <= $max; $i++) {
228
229            if($this->cartSession[$productTypeId][$i]['id'] == $id) {
230                $val = $this->cartSession[$productTypeId][$i]['quantity'] + $quantity;
231                if(strlen($val) <= INT_LEN) {
232                    $this->cartSession[$productTypeId][$i]['quantity'] += $quantity;
233                }
234                $find = true;
235            }
236        }
237        if(!$find) {
238            $this->cartSession[$productTypeId][$max+1]['id'] = $id;
239            $this->cartSession[$productTypeId][$max+1]['quantity'] = $quantity;
240            $this->cartSession[$productTypeId][$max+1]['cart_no'] = $this->getNextCartID($productTypeId);
241        }
242    }
243
244    // 前頁のURLを記録しておく
245    function setPrevURL($url) {
246        // 前頁として記録しないページを指定する。
247        $arrExclude = array(
248            "/shopping/"
249        );
250        $exclude = false;
251        // ページチェックを行う。
252        foreach($arrExclude as $val) {
253            if(preg_match("/" . preg_quote($val) . "/", $url)) {
254                $exclude = true;
255                break;
256            }
257        }
258        // 除外ページでない場合は、前頁として記録する。
259        if(!$exclude) {
260            $_SESSION['prev_url'] = $url;
261        }
262    }
263
264    // 前頁のURLを取得する
265    function getPrevURL() {
266        return isset($_SESSION['prev_url']) ? $_SESSION['prev_url'] : "";
267    }
268
269    // キーが一致した商品の削除
270    function delProductKey($keyname, $val, $productTypeId) {
271        $max = count($this->cartSession[$productTypeId]);
272        for($i = 0; $i < $max; $i++) {
273            if($this->cartSession[$productTypeId][$i][$keyname] == $val) {
274                unset($this->cartSession[$productTypeId][$i]);
275            }
276        }
277    }
278
279    function setValue($key, $val, $productTypeId) {
280        $this->cartSession[$productTypeId][$key] = $val;
281    }
282
283    function getValue($key, $productTypeId) {
284        return $this->cartSession[$productTypeId][$key];
285    }
286
287    function getCartList($productTypeId) {
288        $objProduct = new SC_Product();
289        $max = $this->getMax($productTypeId);
290        $arrRet = array();
291        for($i = 0; $i <= $max; $i++) {
292            if(isset($this->cartSession[$productTypeId][$i]['cart_no'])
293               && $this->cartSession[$productTypeId][$i]['cart_no'] != "") {
294
295                if (SC_Utils_Ex::isBlank($this->cartSession[$productTypeId][$i]['productsClass'])) {
296                    $this->cartSession[$productTypeId][$i]['productsClass'] =&
297                            $objProduct->getDetailAndProductsClass(
298                                    $this->cartSession[$productTypeId][$i]['id']);
299                }
300
301                $price = $this->cartSession[$productTypeId][$i]['productsClass']['price02'];
302                $this->cartSession[$productTypeId][$i]['price'] = $price;
303
304                $this->cartSession[$productTypeId][$i]['point_rate'] =
305                        $this->cartSession[$productTypeId][$i]['productsClass']['point_rate'];
306
307
308                $quantity = $this->cartSession[$productTypeId][$i]['quantity'];
309                $pre_tax = SC_Helper_DB_Ex::sfPreTax($price);
310                $total = $pre_tax * $quantity;
311
312                $this->cartSession[$productTypeId][$i]['total_pretax'] = $total;
313
314                $arrRet[] =& $this->cartSession[$productTypeId][$i];
315            }
316        }
317        return $arrRet;
318    }
319
320    // カート内にある商品IDを全て取得する
321    function getAllProductID($productTypeId) {
322        $max = $this->getMax($productTypeId);
323        for($i = 0; $i <= $max; $i++) {
324            if($this->cartSession[$productTypeId][$i]['cart_no'] != "") {
325                $arrRet[] = $this->cartSession[$productTypeId][$i]['id'][0];
326            }
327        }
328        return $arrRet;
329    }
330
331    function delAllProducts($productTypeId) {
332        $max = $this->getMax($productTypeId);
333        for($i = 0; $i <= $max; $i++) {
334            unset($this->cartSession[$productTypeId][$i]);
335        }
336    }
337
338    // 商品の削除
339    function delProduct($cart_no, $productTypeId) {
340        $max = $this->getMax($productTypeId);
341        for($i = 0; $i <= $max; $i++) {
342            if($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) {
343                unset($this->cartSession[$productTypeId][$i]);
344            }
345        }
346    }
347
348    // 数量の増加
349    function upQuantity($cart_no, $productTypeId) {
350        $quantity = $this->getQuantity($cart_no, $productTypeId);
351        if (strlen($quantity + 1) <= INT_LEN) {
352            $this->setQuantity($quantity + 1, $cart_no, $productTypeId);
353        }
354    }
355
356    // 数量の減少
357    function downQuantity($cart_no, $productTypeId) {
358        $quantity = $this->getQuantity($cart_no, $productTypeId);
359        if ($quantity > 1) {
360            $this->setQuantity($quantity - 1, $cart_no, $productTypeId);
361        }
362    }
363
364    function getQuantity($cart_no, $productTypeId) {
365        $max = $this->getMax($productTypeId);
366        for ($i = 0; $i <= $max; $i++) {
367            if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) {
368                return $this->cartSession[$productTypeId][$i]['quantity'];
369            }
370        }
371    }
372
373    function setQuantity($quantity, $cart_no, $productTypeId) {
374        $max = $this->getMax($productTypeId);
375        for ($i = 0; $i <= $max; $i++) {
376            if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) {
377                $this->cartSession[$productTypeId][$i]['quantity'] = $quantity;
378            }
379        }
380    }
381
382    function getProductClassId($cart_no, $productTypeId) {
383        for ($i = 0; $i <= $max; $i++) {
384            if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) {
385                return $this->cartSession[$productTypeId][$i]['id'];
386            }
387        }
388    }
389
390    /**
391     * カート内の商品の妥当性をチェックする.
392     *
393     * エラーが発生した場合は, 商品をカート内から削除又は数量を調整し,
394     * エラーメッセージを返す.
395     *
396     * 1. 削除/非表示商品のチェック
397     * 2. 商品購入制限数のチェック
398     * 3. 在庫数チェック
399     *
400     * @param string $key 商品種別ID
401     * @return string エラーが発生した場合はエラーメッセージ
402     */
403    function checkProducts($productTypeId) {
404        $objProduct = new SC_Product();
405        $tpl_message = "";
406
407        // カート内の情報を取得
408        $items = $this->getCartList($productTypeId);
409        foreach (array_keys($items) as $key) {
410            $item =& $items[$key];
411            $product =& $item['productsClass'];
412            /*
413             * 表示/非表示商品のチェック
414             */
415            if (SC_Utils_Ex::isBlank($product)) {
416                $this->delProduct($item['cart_no'], $productTypeId);
417                $tpl_message .= "※ 現時点で販売していない商品が含まれておりました。該当商品をカートから削除しました。\n";
418            }
419
420            /*
421             * 商品購入制限数, 在庫数のチェック
422             */
423            $limit = $objProduct->getBuyLimit($product);
424            if (!is_null($limit) && $item['quantity'] > $limit) {
425                if ($limit > 0) {
426                    $this->setProductValue($item['id'], 'quantity', $limit, $productTypeId);
427                    $tpl_message .= "※「" . $product['name'] . "」は販売制限(または在庫が不足)しております。一度に数量{$limit}以上の購入はできません。\n";
428                } else {
429                    $this->delProduct($item['cart_no'], $productTypeId);
430                    $tpl_message .= "※「" . $product['name'] . "」は売り切れました。\n";
431                    continue;
432                }
433            }
434        }
435        return $tpl_message;
436    }
437
438    function getKeys() {
439        return array_keys($this->cartSession);
440    }
441
442    function registerKey($key) {
443        $_SESSION['cartKey'] = $key;
444    }
445
446    function unsetKey() {
447        unset($_SESSION['cartKey']);
448    }
449}
450?>
Note: See TracBrowser for help on using the repository browser.