1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2012 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 | /** |
---|
25 | * カートセッション管理クラス |
---|
26 | * |
---|
27 | * @author LOCKON CO.,LTD. |
---|
28 | * @version $Id$ |
---|
29 | */ |
---|
30 | class SC_CartSession { |
---|
31 | /** ユニークIDを指定する. */ |
---|
32 | var $key_tmp; |
---|
33 | |
---|
34 | /** カートのセッション変数. */ |
---|
35 | var $cartSession; |
---|
36 | |
---|
37 | /* コンストラクタ */ |
---|
38 | function __construct($cartKey = 'cart') { |
---|
39 | if (!isset($_SESSION[$cartKey])) { |
---|
40 | $_SESSION[$cartKey] = array(); |
---|
41 | } |
---|
42 | $this->cartSession =& $_SESSION[$cartKey]; |
---|
43 | } |
---|
44 | |
---|
45 | // 商品購入処理中のロック |
---|
46 | function saveCurrentCart($key_tmp, $productTypeId) { |
---|
47 | $this->key_tmp = 'savecart_' . $key_tmp; |
---|
48 | // すでに情報がなければ現状のカート情報を記録しておく |
---|
49 | if (count($_SESSION[$this->key_tmp]) == 0) { |
---|
50 | $_SESSION[$this->key_tmp] = $this->cartSession[$productTypeId]; |
---|
51 | } |
---|
52 | // 1世代古いコピー情報は、削除しておく |
---|
53 | foreach ($_SESSION as $key => $value) { |
---|
54 | if ($key != $this->key_tmp && preg_match('/^savecart_/', $key)) { |
---|
55 | unset($_SESSION[$key]); |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | // 商品購入中の変更があったかをチェックする。 |
---|
61 | function getCancelPurchase($productTypeId) { |
---|
62 | $ret = isset($this->cartSession[$productTypeId]['cancel_purchase']) |
---|
63 | ? $this->cartSession[$productTypeId]['cancel_purchase'] : ''; |
---|
64 | $this->cartSession[$productTypeId]['cancel_purchase'] = false; |
---|
65 | return $ret; |
---|
66 | } |
---|
67 | |
---|
68 | // 購入処理中に商品に変更がなかったかを判定 |
---|
69 | function checkChangeCart($productTypeId) { |
---|
70 | $change = false; |
---|
71 | $max = $this->getMax($productTypeId); |
---|
72 | for ($i = 1; $i <= $max; $i++) { |
---|
73 | if ($this->cartSession[$productTypeId][$i]['quantity'] |
---|
74 | != $_SESSION[$this->key_tmp][$i]['quantity']) { |
---|
75 | |
---|
76 | $change = true; |
---|
77 | break; |
---|
78 | } |
---|
79 | if ($this->cartSession[$productTypeId][$i]['id'] |
---|
80 | != $_SESSION[$this->key_tmp][$i]['id']) { |
---|
81 | |
---|
82 | $change = true; |
---|
83 | break; |
---|
84 | } |
---|
85 | } |
---|
86 | if ($change) { |
---|
87 | // 一時カートのクリア |
---|
88 | unset($_SESSION[$this->key_tmp]); |
---|
89 | $this->cartSession[$productTypeId]['cancel_purchase'] = true; |
---|
90 | } else { |
---|
91 | $this->cartSession[$productTypeId]['cancel_purchase'] = false; |
---|
92 | } |
---|
93 | return $this->cartSession[$productTypeId]['cancel_purchase']; |
---|
94 | } |
---|
95 | |
---|
96 | // 次に割り当てるカートのIDを取得する |
---|
97 | function getNextCartID($productTypeId) { |
---|
98 | $count = array(); |
---|
99 | foreach ($this->cartSession[$productTypeId] as $key => $value) { |
---|
100 | $count[] = $this->cartSession[$productTypeId][$key]['cart_no']; |
---|
101 | } |
---|
102 | return max($count) + 1; |
---|
103 | } |
---|
104 | |
---|
105 | /** |
---|
106 | * 商品ごとの合計価格 |
---|
107 | * XXX 実際には、「商品」ではなく、「カートの明細行(≒商品規格)」のような気がします。 |
---|
108 | * |
---|
109 | * @param integer $id |
---|
110 | * @return string 商品ごとの合計価格(税込み) |
---|
111 | * @deprecated SC_CartSession::getCartList() を使用してください |
---|
112 | */ |
---|
113 | function getProductTotal($id, $productTypeId) { |
---|
114 | $max = $this->getMax($productTypeId); |
---|
115 | for ($i = 0; $i <= $max; $i++) { |
---|
116 | if (isset($this->cartSession[$productTypeId][$i]['id']) |
---|
117 | && $this->cartSession[$productTypeId][$i]['id'] == $id |
---|
118 | ) { |
---|
119 | // 税込み合計 |
---|
120 | $price = $this->cartSession[$productTypeId][$i]['price']; |
---|
121 | $quantity = $this->cartSession[$productTypeId][$i]['quantity']; |
---|
122 | $incTax = SC_Helper_DB_Ex::sfCalcIncTax($price); |
---|
123 | $total = $incTax * $quantity; |
---|
124 | return $total; |
---|
125 | } |
---|
126 | } |
---|
127 | return 0; |
---|
128 | } |
---|
129 | |
---|
130 | // 値のセット |
---|
131 | function setProductValue($id, $key, $val, $productTypeId) { |
---|
132 | $max = $this->getMax($productTypeId); |
---|
133 | for ($i = 0; $i <= $max; $i++) { |
---|
134 | if (isset($this->cartSession[$productTypeId][$i]['id']) |
---|
135 | && $this->cartSession[$productTypeId][$i]['id'] == $id |
---|
136 | ) { |
---|
137 | $this->cartSession[$productTypeId][$i][$key] = $val; |
---|
138 | } |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | // カート内商品の最大要素番号を取得する。 |
---|
143 | function getMax($productTypeId) { |
---|
144 | $max = 0; |
---|
145 | if (count($this->cartSession[$productTypeId]) > 0) { |
---|
146 | foreach ($this->cartSession[$productTypeId] as $key => $value) { |
---|
147 | if (is_numeric($key)) { |
---|
148 | if ($max < $key) { |
---|
149 | $max = $key; |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | } |
---|
154 | return $max; |
---|
155 | } |
---|
156 | |
---|
157 | // カート内商品数量の合計 |
---|
158 | function getTotalQuantity($productTypeId) { |
---|
159 | $total = 0; |
---|
160 | $max = $this->getMax($productTypeId); |
---|
161 | for ($i = 0; $i <= $max; $i++) { |
---|
162 | $total+= $this->cartSession[$productTypeId][$i]['quantity']; |
---|
163 | } |
---|
164 | return $total; |
---|
165 | } |
---|
166 | |
---|
167 | // 全商品の合計価格 |
---|
168 | function getAllProductsTotal($productTypeId) { |
---|
169 | // 税込み合計 |
---|
170 | $total = 0; |
---|
171 | $max = $this->getMax($productTypeId); |
---|
172 | for ($i = 0; $i <= $max; $i++) { |
---|
173 | |
---|
174 | if (!isset($this->cartSession[$productTypeId][$i]['price'])) { |
---|
175 | $this->cartSession[$productTypeId][$i]['price'] = ''; |
---|
176 | } |
---|
177 | |
---|
178 | $price = $this->cartSession[$productTypeId][$i]['price']; |
---|
179 | |
---|
180 | if (!isset($this->cartSession[$productTypeId][$i]['quantity'])) { |
---|
181 | $this->cartSession[$productTypeId][$i]['quantity'] = ''; |
---|
182 | } |
---|
183 | $quantity = $this->cartSession[$productTypeId][$i]['quantity']; |
---|
184 | |
---|
185 | $incTax = SC_Helper_DB_Ex::sfCalcIncTax($price); |
---|
186 | $total+= ($incTax * $quantity); |
---|
187 | } |
---|
188 | return $total; |
---|
189 | } |
---|
190 | |
---|
191 | // 全商品の合計税金 |
---|
192 | function getAllProductsTax($productTypeId) { |
---|
193 | // 税合計 |
---|
194 | $total = 0; |
---|
195 | $max = $this->getMax($productTypeId); |
---|
196 | for ($i = 0; $i <= $max; $i++) { |
---|
197 | $price = $this->cartSession[$productTypeId][$i]['price']; |
---|
198 | $quantity = $this->cartSession[$productTypeId][$i]['quantity']; |
---|
199 | $tax = SC_Helper_DB_Ex::sfTax($price); |
---|
200 | $total+= ($tax * $quantity); |
---|
201 | } |
---|
202 | return $total; |
---|
203 | } |
---|
204 | |
---|
205 | // 全商品の合計ポイント |
---|
206 | function getAllProductsPoint($productTypeId) { |
---|
207 | // ポイント合計 |
---|
208 | $total = 0; |
---|
209 | if (USE_POINT !== false) { |
---|
210 | $max = $this->getMax($productTypeId); |
---|
211 | for ($i = 0; $i <= $max; $i++) { |
---|
212 | $price = $this->cartSession[$productTypeId][$i]['price']; |
---|
213 | $quantity = $this->cartSession[$productTypeId][$i]['quantity']; |
---|
214 | |
---|
215 | if (!isset($this->cartSession[$productTypeId][$i]['point_rate'])) { |
---|
216 | $this->cartSession[$productTypeId][$i]['point_rate'] = ''; |
---|
217 | } |
---|
218 | $point_rate = $this->cartSession[$productTypeId][$i]['point_rate']; |
---|
219 | |
---|
220 | if (!isset($this->cartSession[$productTypeId][$i]['id'][0])) { |
---|
221 | $this->cartSession[$productTypeId][$i]['id'][0] = ''; |
---|
222 | } |
---|
223 | $point = SC_Utils_Ex::sfPrePoint($price, $point_rate); |
---|
224 | $total+= ($point * $quantity); |
---|
225 | } |
---|
226 | } |
---|
227 | return $total; |
---|
228 | } |
---|
229 | |
---|
230 | // カートへの商品追加 |
---|
231 | function addProduct($product_class_id, $quantity) { |
---|
232 | $objProduct = new SC_Product_Ex(); |
---|
233 | $arrProduct = $objProduct->getProductsClass($product_class_id); |
---|
234 | $productTypeId = $arrProduct['product_type_id']; |
---|
235 | $find = false; |
---|
236 | $max = $this->getMax($productTypeId); |
---|
237 | for ($i = 0; $i <= $max; $i++) { |
---|
238 | |
---|
239 | if ($this->cartSession[$productTypeId][$i]['id'] == $product_class_id) { |
---|
240 | $val = $this->cartSession[$productTypeId][$i]['quantity'] + $quantity; |
---|
241 | if (strlen($val) <= INT_LEN) { |
---|
242 | $this->cartSession[$productTypeId][$i]['quantity'] += $quantity; |
---|
243 | } |
---|
244 | $find = true; |
---|
245 | } |
---|
246 | } |
---|
247 | if (!$find) { |
---|
248 | $this->cartSession[$productTypeId][$max+1]['id'] = $product_class_id; |
---|
249 | $this->cartSession[$productTypeId][$max+1]['quantity'] = $quantity; |
---|
250 | $this->cartSession[$productTypeId][$max+1]['cart_no'] = $this->getNextCartID($productTypeId); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | // 前頁のURLを記録しておく |
---|
255 | function setPrevURL($url, $excludePaths = array()) { |
---|
256 | // 前頁として記録しないページを指定する。 |
---|
257 | $arrExclude = array( |
---|
258 | '/shopping/' |
---|
259 | ); |
---|
260 | $arrExclude = array_merge($arrExclude, $excludePaths); |
---|
261 | $exclude = false; |
---|
262 | // ページチェックを行う。 |
---|
263 | foreach ($arrExclude as $val) { |
---|
264 | if (preg_match('|' . preg_quote($val) . '|', $url)) { |
---|
265 | $exclude = true; |
---|
266 | break; |
---|
267 | } |
---|
268 | } |
---|
269 | // 除外ページでない場合は、前頁として記録する。 |
---|
270 | if (!$exclude) { |
---|
271 | $_SESSION['prev_url'] = $url; |
---|
272 | } |
---|
273 | } |
---|
274 | |
---|
275 | // 前頁のURLを取得する |
---|
276 | function getPrevURL() { |
---|
277 | return isset($_SESSION['prev_url']) ? $_SESSION['prev_url'] : ''; |
---|
278 | } |
---|
279 | |
---|
280 | // キーが一致した商品の削除 |
---|
281 | function delProductKey($keyname, $val, $productTypeId) { |
---|
282 | $max = count($this->cartSession[$productTypeId]); |
---|
283 | for ($i = 0; $i < $max; $i++) { |
---|
284 | if ($this->cartSession[$productTypeId][$i][$keyname] == $val) { |
---|
285 | unset($this->cartSession[$productTypeId][$i]); |
---|
286 | } |
---|
287 | } |
---|
288 | } |
---|
289 | |
---|
290 | function setValue($key, $val, $productTypeId) { |
---|
291 | $this->cartSession[$productTypeId][$key] = $val; |
---|
292 | } |
---|
293 | |
---|
294 | function getValue($key, $productTypeId) { |
---|
295 | return $this->cartSession[$productTypeId][$key]; |
---|
296 | } |
---|
297 | |
---|
298 | /** |
---|
299 | * セッション中の商品情報データの調整。 |
---|
300 | * productsClass項目から、不必要な項目を削除する。 |
---|
301 | */ |
---|
302 | function adjustSessionProductsClass(&$arrProductsClass) { |
---|
303 | $arrNecessaryItems = array( |
---|
304 | 'product_id' => true, |
---|
305 | 'product_class_id' => true, |
---|
306 | 'name' => true, |
---|
307 | 'price02' => true, |
---|
308 | 'point_rate' => true, |
---|
309 | 'main_list_image' => true, |
---|
310 | 'main_image' => true, |
---|
311 | 'product_code' => true, |
---|
312 | 'stock' => true, |
---|
313 | 'stock_unlimited' => true, |
---|
314 | 'sale_limit' => true, |
---|
315 | 'class_name1' => true, |
---|
316 | 'classcategory_name1' => true, |
---|
317 | 'class_name2' => true, |
---|
318 | 'classcategory_name2' => true, |
---|
319 | ); |
---|
320 | |
---|
321 | // 必要な項目以外を削除。 |
---|
322 | foreach ($arrProductsClass as $key => $value) { |
---|
323 | if (!isset($arrNecessaryItems[$key])) { |
---|
324 | unset($arrProductsClass[$key]); |
---|
325 | } |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | /** |
---|
330 | * 商品種別ごとにカート内商品の一覧を取得する. |
---|
331 | * |
---|
332 | * @param integer $productTypeId 商品種別ID |
---|
333 | * @return array カート内商品一覧の配列 |
---|
334 | */ |
---|
335 | function getCartList($productTypeId) { |
---|
336 | $objProduct = new SC_Product_Ex(); |
---|
337 | $max = $this->getMax($productTypeId); |
---|
338 | $arrRet = array(); |
---|
339 | for ($i = 0; $i <= $max; $i++) { |
---|
340 | if (isset($this->cartSession[$productTypeId][$i]['cart_no']) |
---|
341 | && $this->cartSession[$productTypeId][$i]['cart_no'] != '') { |
---|
342 | |
---|
343 | // 商品情報は常に取得 |
---|
344 | // TODO 同一インスタンス内では1回のみ呼ぶようにしたい |
---|
345 | $this->cartSession[$productTypeId][$i]['productsClass'] |
---|
346 | =& $objProduct->getDetailAndProductsClass($this->cartSession[$productTypeId][$i]['id']); |
---|
347 | |
---|
348 | $price = $this->cartSession[$productTypeId][$i]['productsClass']['price02']; |
---|
349 | $this->cartSession[$productTypeId][$i]['price'] = $price; |
---|
350 | |
---|
351 | $this->cartSession[$productTypeId][$i]['point_rate'] |
---|
352 | = $this->cartSession[$productTypeId][$i]['productsClass']['point_rate']; |
---|
353 | |
---|
354 | $quantity = $this->cartSession[$productTypeId][$i]['quantity']; |
---|
355 | $incTax = SC_Helper_DB_Ex::sfCalcIncTax($price); |
---|
356 | $total = $incTax * $quantity; |
---|
357 | |
---|
358 | $this->cartSession[$productTypeId][$i]['total_inctax'] = $total; |
---|
359 | |
---|
360 | $arrRet[] = $this->cartSession[$productTypeId][$i]; |
---|
361 | |
---|
362 | // セッション変数のデータ量を抑制するため、一部の商品情報を切り捨てる |
---|
363 | // XXX 上で「常に取得」するのだから、丸ごと切り捨てて良さそうにも感じる。 |
---|
364 | $this->adjustSessionProductsClass($this->cartSession[$productTypeId][$i]['productsClass']); |
---|
365 | } |
---|
366 | } |
---|
367 | return $arrRet; |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * すべてのカートの内容を取得する. |
---|
372 | * |
---|
373 | * @return array すべてのカートの内容 |
---|
374 | */ |
---|
375 | function getAllCartList() { |
---|
376 | $results = array(); |
---|
377 | $cartKeys = $this->getKeys(); |
---|
378 | $i = 0; |
---|
379 | foreach ($cartKeys as $key) { |
---|
380 | $cartItems = $this->getCartList($key); |
---|
381 | foreach ($cartItems as $itemKey => $itemValue) { |
---|
382 | $cartItem =& $cartItems[$itemKey]; |
---|
383 | $results[$key][$i] =& $cartItem; |
---|
384 | $i++; |
---|
385 | } |
---|
386 | } |
---|
387 | return $results; |
---|
388 | } |
---|
389 | |
---|
390 | /** |
---|
391 | * @deprected getAllProductClassID を使用して下さい |
---|
392 | */ |
---|
393 | function getAllProductID($productTypeId) { |
---|
394 | trigger_error(t('SC_CartSession_001'), E_USER_ERROR); |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * カート内にある商品規格IDを全て取得する. |
---|
399 | * |
---|
400 | * @param integer $productTypeId 商品種別ID |
---|
401 | * @return array 商品規格ID の配列 |
---|
402 | */ |
---|
403 | function getAllProductClassID($productTypeId) { |
---|
404 | $max = $this->getMax($productTypeId); |
---|
405 | $productClassIDs = array(); |
---|
406 | for ($i = 0; $i <= $max; $i++) { |
---|
407 | if ($this->cartSession[$productTypeId][$i]['cart_no'] != '') { |
---|
408 | $productClassIDs[] = $this->cartSession[$productTypeId][$i]['id']; |
---|
409 | } |
---|
410 | } |
---|
411 | return $productClassIDs; |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | * 商品種別ID を指定して, カート内の商品をすべて削除する. |
---|
416 | * |
---|
417 | * @param integer $productTypeId 商品種別ID |
---|
418 | * @return void |
---|
419 | */ |
---|
420 | function delAllProducts($productTypeId) { |
---|
421 | $max = $this->getMax($productTypeId); |
---|
422 | for ($i = 0; $i <= $max; $i++) { |
---|
423 | unset($this->cartSession[$productTypeId][$i]); |
---|
424 | } |
---|
425 | } |
---|
426 | |
---|
427 | // 商品の削除 |
---|
428 | function delProduct($cart_no, $productTypeId) { |
---|
429 | $max = $this->getMax($productTypeId); |
---|
430 | for ($i = 0; $i <= $max; $i++) { |
---|
431 | if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) { |
---|
432 | unset($this->cartSession[$productTypeId][$i]); |
---|
433 | } |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | // 数量の増加 |
---|
438 | function upQuantity($cart_no, $productTypeId) { |
---|
439 | $quantity = $this->getQuantity($cart_no, $productTypeId); |
---|
440 | if (strlen($quantity + 1) <= INT_LEN) { |
---|
441 | $this->setQuantity($quantity + 1, $cart_no, $productTypeId); |
---|
442 | } |
---|
443 | } |
---|
444 | |
---|
445 | // 数量の減少 |
---|
446 | function downQuantity($cart_no, $productTypeId) { |
---|
447 | $quantity = $this->getQuantity($cart_no, $productTypeId); |
---|
448 | if ($quantity > 1) { |
---|
449 | $this->setQuantity($quantity - 1, $cart_no, $productTypeId); |
---|
450 | } |
---|
451 | } |
---|
452 | |
---|
453 | /** |
---|
454 | * カート番号と商品種別IDを指定して, 数量を取得する. |
---|
455 | * |
---|
456 | * @param integer $cart_no カート番号 |
---|
457 | * @param integer $productTypeId 商品種別ID |
---|
458 | * @return integer 該当商品規格の数量 |
---|
459 | */ |
---|
460 | function getQuantity($cart_no, $productTypeId) { |
---|
461 | $max = $this->getMax($productTypeId); |
---|
462 | for ($i = 0; $i <= $max; $i++) { |
---|
463 | if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) { |
---|
464 | return $this->cartSession[$productTypeId][$i]['quantity']; |
---|
465 | } |
---|
466 | } |
---|
467 | } |
---|
468 | |
---|
469 | /** |
---|
470 | * カート番号と商品種別IDを指定して, 数量を設定する. |
---|
471 | * |
---|
472 | * @param integer $quantity 設定する数量 |
---|
473 | * @param integer $cart_no カート番号 |
---|
474 | * @param integer $productTypeId 商品種別ID |
---|
475 | * @retrun void |
---|
476 | */ |
---|
477 | function setQuantity($quantity, $cart_no, $productTypeId) { |
---|
478 | $max = $this->getMax($productTypeId); |
---|
479 | for ($i = 0; $i <= $max; $i++) { |
---|
480 | if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) { |
---|
481 | $this->cartSession[$productTypeId][$i]['quantity'] = $quantity; |
---|
482 | } |
---|
483 | } |
---|
484 | } |
---|
485 | |
---|
486 | /** |
---|
487 | * カート番号と商品種別IDを指定して, 商品規格IDを取得する. |
---|
488 | * |
---|
489 | * @param integer $cart_no カート番号 |
---|
490 | * @param integer $productTypeId 商品種別ID |
---|
491 | * @return integer 商品規格ID |
---|
492 | */ |
---|
493 | function getProductClassId($cart_no, $productTypeId) { |
---|
494 | for ($i = 0; $i < count($this->cartSession[$productTypeId]); $i++) { |
---|
495 | if ($this->cartSession[$productTypeId][$i]['cart_no'] == $cart_no) { |
---|
496 | return $this->cartSession[$productTypeId][$i]['id']; |
---|
497 | } |
---|
498 | } |
---|
499 | } |
---|
500 | |
---|
501 | /** |
---|
502 | * カート内の商品の妥当性をチェックする. |
---|
503 | * |
---|
504 | * エラーが発生した場合は, 商品をカート内から削除又は数量を調整し, |
---|
505 | * エラーメッセージを返す. |
---|
506 | * |
---|
507 | * 1. 商品種別に関連づけられた配送業者の存在チェック |
---|
508 | * 2. 削除/非表示商品のチェック |
---|
509 | * 3. 販売制限数のチェック |
---|
510 | * 4. 在庫数チェック |
---|
511 | * |
---|
512 | * @param string $productTypeId 商品種別ID |
---|
513 | * @return string エラーが発生した場合はエラーメッセージ |
---|
514 | */ |
---|
515 | function checkProducts($productTypeId) { |
---|
516 | $objProduct = new SC_Product_Ex(); |
---|
517 | $tpl_message = ''; |
---|
518 | |
---|
519 | // カート内の情報を取得 |
---|
520 | $arrItems = $this->getCartList($productTypeId); |
---|
521 | foreach ($arrItems as &$arrItem) { |
---|
522 | $product =& $arrItem['productsClass']; |
---|
523 | /* |
---|
524 | * 表示/非表示商品のチェック |
---|
525 | */ |
---|
526 | if (SC_Utils_Ex::isBlank($product) || $product['status'] != 1) { |
---|
527 | $this->delProduct($arrItem['cart_no'], $productTypeId); |
---|
528 | $tpl_message .= t('SC_CartSession_002'); |
---|
529 | } else { |
---|
530 | |
---|
531 | /* |
---|
532 | * 配送業者のチェック |
---|
533 | */ |
---|
534 | $arrDeliv = SC_Helper_Purchase_Ex::getDeliv($productTypeId); |
---|
535 | if (SC_Utils_Ex::isBlank($arrDeliv)) { |
---|
536 | $tpl_message .= t('SC_CartSession_003', array('T_PRODUCT' => $product['name'])); |
---|
537 | $this->delProduct($arrItem['cart_no'], $productTypeId); |
---|
538 | } |
---|
539 | |
---|
540 | /* |
---|
541 | * 販売制限数, 在庫数のチェック |
---|
542 | */ |
---|
543 | $limit = $objProduct->getBuyLimit($product); |
---|
544 | if (!is_null($limit) && $arrItem['quantity'] > $limit) { |
---|
545 | if ($limit > 0) { |
---|
546 | $this->setProductValue($arrItem['id'], 'quantity', $limit, $productTypeId); |
---|
547 | $total_inctax = SC_Helper_DB_Ex::sfCalcIncTax($arrItem['price']) * $limit; |
---|
548 | $this->setProductValue($arrItem['id'], 'total_inctax', $total_inctax, $productTypeId); |
---|
549 | $tpl_message .= t('SC_CartSession_004', array('T_PRODUCT' => $product['name'], 'T_LIMIT' => $limit)); |
---|
550 | } else { |
---|
551 | $this->delProduct($arrItem['cart_no'], $productTypeId); |
---|
552 | $tpl_message .= t('SC_CartSession_005', array('T_PRODUCT' => $product['name'])); |
---|
553 | continue; |
---|
554 | } |
---|
555 | } |
---|
556 | } |
---|
557 | } |
---|
558 | return $tpl_message; |
---|
559 | } |
---|
560 | |
---|
561 | /** |
---|
562 | * 送料無料条件を満たすかどうかチェックする |
---|
563 | * |
---|
564 | * @param integer $productTypeId 商品種別ID |
---|
565 | * @return boolean 送料無料の場合 true |
---|
566 | */ |
---|
567 | function isDelivFree($productTypeId) { |
---|
568 | $objDb = new SC_Helper_DB_Ex(); |
---|
569 | |
---|
570 | $subtotal = $this->getAllProductsTotal($productTypeId); |
---|
571 | |
---|
572 | // 送料無料の購入数が設定されている場合 |
---|
573 | if (DELIV_FREE_AMOUNT > 0) { |
---|
574 | // 商品の合計数量 |
---|
575 | $total_quantity = $this->getTotalQuantity($productTypeId); |
---|
576 | |
---|
577 | if ($total_quantity >= DELIV_FREE_AMOUNT) { |
---|
578 | return true; |
---|
579 | } |
---|
580 | } |
---|
581 | |
---|
582 | // 送料無料条件が設定されている場合 |
---|
583 | $arrInfo = $objDb->sfGetBasisData(); |
---|
584 | if ($arrInfo['free_rule'] > 0) { |
---|
585 | // 小計が送料無料条件以上の場合 |
---|
586 | if ($subtotal >= $arrInfo['free_rule']) { |
---|
587 | return true; |
---|
588 | } |
---|
589 | } |
---|
590 | |
---|
591 | return false; |
---|
592 | } |
---|
593 | |
---|
594 | /** |
---|
595 | * カートの内容を計算する. |
---|
596 | * |
---|
597 | * カートの内容を計算し, 下記のキーを保持する連想配列を返す. |
---|
598 | * |
---|
599 | * - tax: 税額 |
---|
600 | * - subtotal: カート内商品の小計 |
---|
601 | * - deliv_fee: カート内商品の合計送料 |
---|
602 | * - total: 合計金額 |
---|
603 | * - payment_total: お支払い合計 |
---|
604 | * - add_point: 加算ポイント |
---|
605 | * |
---|
606 | * @param integer $productTypeId 商品種別ID |
---|
607 | * @param SC_Customer $objCustomer ログイン中の SC_Customer インスタンス |
---|
608 | * @param integer $use_point 今回使用ポイント |
---|
609 | * @param integer|array $deliv_pref 配送先都道府県ID. |
---|
610 | 複数に配送する場合は都道府県IDの配列 |
---|
611 | * @param integer $charge 手数料 |
---|
612 | * @param integer $discount 値引き |
---|
613 | * @param integer $deliv_id 配送業者ID |
---|
614 | * @return array カートの計算結果の配列 |
---|
615 | */ |
---|
616 | function calculate($productTypeId, &$objCustomer, $use_point = 0, |
---|
617 | $deliv_pref = '', $charge = 0, $discount = 0, $deliv_id = 0 |
---|
618 | ) { |
---|
619 | |
---|
620 | $results = array(); |
---|
621 | $total_point = $this->getAllProductsPoint($productTypeId); |
---|
622 | $results['tax'] = $this->getAllProductsTax($productTypeId); |
---|
623 | $results['subtotal'] = $this->getAllProductsTotal($productTypeId); |
---|
624 | $results['deliv_fee'] = 0; |
---|
625 | |
---|
626 | // 商品ごとの送料を加算 |
---|
627 | if (OPTION_PRODUCT_DELIV_FEE == 1) { |
---|
628 | $cartItems = $this->getCartList($productTypeId); |
---|
629 | foreach ($cartItems as $arrItem) { |
---|
630 | $results['deliv_fee'] += $arrItem['productsClass']['deliv_fee'] * $arrItem['quantity']; |
---|
631 | } |
---|
632 | } |
---|
633 | |
---|
634 | // 配送業者の送料を加算 |
---|
635 | if (OPTION_DELIV_FEE == 1 |
---|
636 | && !SC_Utils_Ex::isBlank($deliv_pref) |
---|
637 | && !SC_Utils_Ex::isBlank($deliv_id)) { |
---|
638 | $results['deliv_fee'] += $this->sfGetDelivFee($deliv_pref, $deliv_id); |
---|
639 | } |
---|
640 | |
---|
641 | // 送料無料チェック |
---|
642 | if ($this->isDelivFree($productTypeId)) { |
---|
643 | $results['deliv_fee'] = 0; |
---|
644 | } |
---|
645 | |
---|
646 | // 合計を計算 |
---|
647 | $results['total'] = $results['subtotal']; |
---|
648 | $results['total'] += $results['deliv_fee']; |
---|
649 | $results['total'] += $charge; |
---|
650 | $results['total'] -= $discount; |
---|
651 | |
---|
652 | // お支払い合計 |
---|
653 | $results['payment_total'] = $results['total'] - $use_point * POINT_VALUE; |
---|
654 | |
---|
655 | // 加算ポイントの計算 |
---|
656 | if (USE_POINT !== false) { |
---|
657 | $results['add_point'] = SC_Helper_DB_Ex::sfGetAddPoint($total_point, $use_point); |
---|
658 | if ($objCustomer != '') { |
---|
659 | // 誕生日月であった場合 |
---|
660 | if ($objCustomer->isBirthMonth()) { |
---|
661 | $results['birth_point'] = BIRTH_MONTH_POINT; |
---|
662 | $results['add_point'] += $results['birth_point']; |
---|
663 | } |
---|
664 | } |
---|
665 | if ($results['add_point'] < 0) { |
---|
666 | $results['add_point'] = 0; |
---|
667 | } |
---|
668 | } |
---|
669 | return $results; |
---|
670 | } |
---|
671 | |
---|
672 | /** |
---|
673 | * カートが保持するキー(商品種別ID)を配列で返す. |
---|
674 | * |
---|
675 | * @return array 商品種別IDの配列 |
---|
676 | */ |
---|
677 | function getKeys() { |
---|
678 | $keys = array_keys($this->cartSession); |
---|
679 | // 数量が 0 の商品種別は削除する |
---|
680 | foreach ($keys as $key) { |
---|
681 | $quantity = $this->getTotalQuantity($key); |
---|
682 | if ($quantity < 1) { |
---|
683 | unset($this->cartSession[$key]); |
---|
684 | } |
---|
685 | } |
---|
686 | return array_keys($this->cartSession); |
---|
687 | } |
---|
688 | |
---|
689 | /** |
---|
690 | * カートに設定された現在のキー(商品種別ID)を登録する. |
---|
691 | * |
---|
692 | * @param integer $key 商品種別ID |
---|
693 | * @return void |
---|
694 | */ |
---|
695 | function registerKey($key) { |
---|
696 | $_SESSION['cartKey'] = $key; |
---|
697 | } |
---|
698 | |
---|
699 | /** |
---|
700 | * カートに設定された現在のキー(商品種別ID)を削除する. |
---|
701 | * |
---|
702 | * @return void |
---|
703 | */ |
---|
704 | function unsetKey() { |
---|
705 | unset($_SESSION['cartKey']); |
---|
706 | } |
---|
707 | |
---|
708 | /** |
---|
709 | * カートに設定された現在のキー(商品種別ID)を取得する. |
---|
710 | * |
---|
711 | * @return integer 商品種別ID |
---|
712 | */ |
---|
713 | function getKey() { |
---|
714 | return $_SESSION['cartKey']; |
---|
715 | } |
---|
716 | |
---|
717 | /** |
---|
718 | * 複数商品種別かどうか. |
---|
719 | * |
---|
720 | * @return boolean カートが複数商品種別の場合 true |
---|
721 | */ |
---|
722 | function isMultiple() { |
---|
723 | return count($this->getKeys()) > 1; |
---|
724 | } |
---|
725 | |
---|
726 | /** |
---|
727 | * 引数の商品種別の商品がカートに含まれるかどうか. |
---|
728 | * |
---|
729 | * @param integer $product_type_id 商品種別ID |
---|
730 | * @return boolean 指定の商品種別がカートに含まれる場合 true |
---|
731 | */ |
---|
732 | function hasProductType($product_type_id) { |
---|
733 | return in_array($product_type_id, $this->getKeys()); |
---|
734 | } |
---|
735 | |
---|
736 | /** |
---|
737 | * 都道府県から配送料金を取得する. |
---|
738 | * |
---|
739 | * @param integer|array $pref_id 都道府県ID 又は都道府県IDの配列 |
---|
740 | * @param integer $deliv_id 配送業者ID |
---|
741 | * @return string 指定の都道府県, 配送業者の配送料金 |
---|
742 | */ |
---|
743 | function sfGetDelivFee($pref_id, $deliv_id = 0) { |
---|
744 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
745 | if (!is_array($pref_id)) { |
---|
746 | $pref_id = array($pref_id); |
---|
747 | } |
---|
748 | $sql = <<< __EOS__ |
---|
749 | SELECT T1.fee AS fee |
---|
750 | FROM dtb_delivfee T1 |
---|
751 | JOIN dtb_deliv T2 |
---|
752 | ON T1.deliv_id = T2.deliv_id |
---|
753 | WHERE T1.pref = ? |
---|
754 | AND T1.deliv_id = ? |
---|
755 | AND T2.del_flg = 0 |
---|
756 | __EOS__; |
---|
757 | $result = 0; |
---|
758 | foreach ($pref_id as $pref) { |
---|
759 | $result += $objQuery->getOne($sql, array($pref, $deliv_id)); |
---|
760 | } |
---|
761 | return $result; |
---|
762 | } |
---|
763 | |
---|
764 | } |
---|