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