|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of EC-CUBE |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright(c) 2000-2015 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
|
|
|
namespace Eccube\Controller; |
|
26
|
|
|
|
|
27
|
|
|
use Eccube\Annotation\Inject; |
|
28
|
|
|
use Eccube\Application; |
|
29
|
|
|
use Eccube\Entity\Cart; |
|
30
|
|
|
use Eccube\Entity\ProductClass; |
|
31
|
|
|
use Eccube\Event\EccubeEvents; |
|
32
|
|
|
use Eccube\Event\EventArgs; |
|
33
|
|
|
use Eccube\Repository\ProductClassRepository; |
|
34
|
|
|
use Eccube\Service\CartService; |
|
35
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseFlow; |
|
36
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
37
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
38
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
39
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
40
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @Route(service=CartController::class) |
|
44
|
|
|
*/ |
|
45
|
|
|
class CartController extends AbstractController |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @Inject("eccube.event.dispatcher") |
|
49
|
|
|
* @var EventDispatcher |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $eventDispatcher; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @Inject(ProductClassRepository::class) |
|
55
|
|
|
* @var ProductClassRepository |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $productClassRepository; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @Inject("eccube.purchase.flow.cart") |
|
61
|
|
|
* @var PurchaseFlow |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $purchaseFlow; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @Inject(CartService::class) |
|
67
|
|
|
* @var CartService |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $cartService; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
|
|
|
|
|
72
|
|
|
* カート画面. |
|
73
|
|
|
* |
|
74
|
|
|
* @Route("/cart", name="cart") |
|
75
|
|
|
* @Template("Cart/index.twig") |
|
76
|
|
|
*/ |
|
|
|
|
|
|
77
|
1 |
|
public function index(Application $app, Request $request) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
// カートを取得して明細の正規化を実行 |
|
80
|
1 |
|
$Carts = $this->cartService->getCarts(); |
|
81
|
|
|
|
|
82
|
1 |
|
$this->execPurchaseFlow($app, $Carts); |
|
83
|
|
|
|
|
84
|
|
|
// TODO itemHolderから取得できるように |
|
85
|
1 |
|
$least = 0; |
|
86
|
|
|
$quantity = 0; |
|
87
|
|
|
$isDeliveryFree = false; |
|
88
|
|
|
|
|
89
|
|
|
$totalQuantity = array_reduce($Carts, function($total, $Cart) { |
|
|
|
|
|
|
90
|
|
|
/** @var Cart $Cart */ |
|
91
|
|
|
$total += $Cart->getQuantity(); |
|
92
|
|
|
return $total; |
|
|
|
|
|
|
93
|
|
|
}, 0); |
|
94
|
|
|
$totalPrice = array_reduce($Carts, function($total, $Cart) { |
|
|
|
|
|
|
95
|
1 |
|
/** @var Cart $Cart */ |
|
96
|
|
|
$total += $Cart->getTotalPrice(); |
|
97
|
1 |
|
return $total; |
|
|
|
|
|
|
98
|
|
|
}, 0); |
|
99
|
|
|
|
|
100
|
|
|
return [ |
|
101
|
|
|
'totalPrice' => $totalPrice, |
|
102
|
1 |
|
'totalQuantity' => $totalQuantity, |
|
103
|
1 |
|
'Carts' => $Carts, |
|
104
|
1 |
|
'least' => $least, |
|
105
|
|
|
'quantity' => $quantity, |
|
106
|
|
|
'is_delivery_free' => $isDeliveryFree, |
|
107
|
1 |
|
]; |
|
108
|
1 |
|
} |
|
109
|
1 |
|
|
|
110
|
1 |
|
protected function execPurchaseFlow(Application $app, $Carts) |
|
111
|
|
|
{ |
|
112
|
|
|
$flowResults = array_map(function($Cart) use ($app) { |
|
|
|
|
|
|
113
|
|
|
return $this->purchaseFlow->calculate($Cart, $app['eccube.purchase.context']()); |
|
114
|
|
|
}, $Carts); |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
// 復旧不可のエラーが発生した場合はカートをクリアして再描画 |
|
118
|
|
|
$hasError = false; |
|
119
|
|
|
foreach ($flowResults as $result) { |
|
120
|
|
|
if ($result->hasError()) { |
|
121
|
|
|
$hasError = true; |
|
122
|
|
|
foreach ($result->getErrors() as $error) { |
|
123
|
|
|
$app->addRequestError($error->getMessage()); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
if ($hasError) { |
|
128
|
|
|
$this->cartService->clear(); |
|
129
|
|
|
$this->cartService->save(); |
|
130
|
|
|
return $app->redirect($app->url('cart')); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->cartService->save(); |
|
134
|
|
|
|
|
135
|
16 |
|
foreach ($flowResults as $index=>$result) { |
|
|
|
|
|
|
136
|
|
|
foreach ($result->getWarning() as $warning) { |
|
137
|
16 |
|
$app->addRequestError($warning->getMessage(), "front.cart.${index}"); |
|
138
|
|
|
} |
|
139
|
16 |
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
16 |
|
/** |
|
|
|
|
|
|
143
|
|
|
* カート明細の加算/減算/削除を行う. |
|
144
|
16 |
|
* |
|
145
|
|
|
* - 加算 |
|
146
|
|
|
* - 明細の個数を1増やす |
|
147
|
|
|
* - 減算 |
|
148
|
|
|
* - 明細の個数を1減らす |
|
149
|
|
|
* - 個数が0になる場合は、明細を削除する |
|
150
|
|
|
* - 削除 |
|
151
|
|
|
* - 明細を削除する |
|
152
|
16 |
|
* |
|
153
|
14 |
|
* @Method("PUT") |
|
154
|
14 |
|
* @Route( |
|
155
|
2 |
|
* path="/cart/{operation}/{productClassId}", |
|
156
|
1 |
|
* name="cart_handle_item", |
|
157
|
1 |
|
* requirements={ |
|
158
|
1 |
|
* "operation": "up|down|remove", |
|
159
|
1 |
|
* "productClassId": "\d+" |
|
160
|
1 |
|
* } |
|
161
|
|
|
* ) |
|
162
|
|
|
*/ |
|
|
|
|
|
|
163
|
|
|
public function handleCartItem(Application $app, Request $request, $operation, $productClassId) |
|
|
|
|
|
|
164
|
16 |
|
{ |
|
165
|
|
|
log_info('カート明細操作開始', ['operation' => $operation, 'product_class_id' => $productClassId]); |
|
166
|
16 |
|
|
|
167
|
|
|
$this->isTokenValid($app); |
|
168
|
|
|
|
|
169
|
16 |
|
/** @var ProductClass $ProductClass */ |
|
170
|
1 |
|
$ProductClass = $this->productClassRepository->find($productClassId); |
|
171
|
1 |
|
|
|
172
|
|
|
if (is_null($ProductClass)) { |
|
173
|
1 |
|
log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation, 'product_class_id' => $productClassId]); |
|
174
|
1 |
|
|
|
175
|
|
|
return $app->redirect($app->url('cart')); |
|
176
|
1 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
// 明細の増減・削除 |
|
179
|
15 |
|
switch ($operation) { |
|
180
|
|
|
case 'up': |
|
181
|
15 |
|
$this->cartService->addProduct($ProductClass, 1); |
|
182
|
|
|
break; |
|
183
|
|
|
case 'down': |
|
184
|
|
|
$this->cartService->addProduct($ProductClass, -1); |
|
185
|
15 |
|
break; |
|
186
|
|
|
case 'remove': |
|
187
|
15 |
|
$this->cartService->removeProduct($ProductClass); |
|
188
|
|
|
break; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
// カートを取得して明細の正規化を実行 |
|
192
|
|
|
$Carts = $this->cartService->getCarts(); |
|
193
|
|
|
$this->execPurchaseFlow($app, $Carts); |
|
194
|
|
|
|
|
195
|
|
|
log_info('カート演算処理終了', ['operation' => $operation, 'product_class_id' => $productClassId]); |
|
196
|
|
|
|
|
197
|
|
|
return $app->redirect($app->url('cart')); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
|
|
|
|
|
201
|
|
|
* カートをロック状態に設定し、購入確認画面へ遷移する. |
|
202
|
|
|
* |
|
203
|
|
|
* @Route("/cart/buystep/{index}", name="cart_buystep", requirements={"index" = "\d+"}, defaults={"index" = 0}) |
|
204
|
|
|
*/ |
|
|
|
|
|
|
205
|
|
|
public function buystep(Application $app, Request $request, $index) |
|
206
|
|
|
{ |
|
207
|
|
|
// FRONT_CART_BUYSTEP_INITIALIZE |
|
208
|
|
|
$event = new EventArgs( |
|
209
|
|
|
array(), |
|
210
|
|
|
$request |
|
211
|
|
|
); |
|
212
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE, $event); |
|
213
|
|
|
|
|
214
|
|
|
$this->cartService->setPrimary($index); |
|
215
|
|
|
$this->cartService->lock(); |
|
216
|
|
|
$this->cartService->save(); |
|
217
|
|
|
|
|
218
|
|
|
// FRONT_CART_BUYSTEP_COMPLETE |
|
219
|
|
|
$event = new EventArgs( |
|
220
|
|
|
array(), |
|
221
|
|
|
$request |
|
222
|
|
|
); |
|
223
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::FRONT_CART_BUYSTEP_COMPLETE, $event); |
|
224
|
|
|
|
|
225
|
|
|
if ($event->hasResponse()) { |
|
226
|
|
|
return $event->getResponse(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $app->redirect($app->url('shopping')); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|