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