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