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\Service; |
26
|
|
|
|
27
|
|
|
use Doctrine\ORM\EntityManager; |
28
|
|
|
use Eccube\Annotation\Inject; |
29
|
|
|
use Eccube\Annotation\Service; |
30
|
|
|
use Eccube\Entity\Cart; |
31
|
|
|
use Eccube\Entity\CartItem; |
32
|
|
|
use Eccube\Entity\ItemHolderInterface; |
33
|
|
|
use Eccube\Entity\ProductClass; |
34
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @Service |
38
|
|
|
*/ |
39
|
|
|
class CartService |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var Session |
43
|
|
|
* @Inject("session") |
44
|
|
|
*/ |
45
|
|
|
protected $session; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var EntityManager |
49
|
|
|
* @Inject("orm.em") |
50
|
|
|
*/ |
51
|
|
|
protected $em; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var ItemHolderInterface |
55
|
|
|
*/ |
56
|
|
|
protected $cart; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return ItemHolderInterface|Cart |
60
|
|
|
*/ |
61
|
91 |
|
public function getCart() |
62
|
|
|
{ |
63
|
91 |
|
if (is_null($this->cart)) { |
64
|
91 |
|
$this->cart = $this->session->get('cart', new Cart()); |
65
|
91 |
|
$this->loadItems(); |
66
|
|
|
} |
67
|
|
|
|
68
|
91 |
|
return $this->cart; |
69
|
|
|
} |
70
|
|
|
|
71
|
91 |
|
protected function loadItems() |
72
|
|
|
{ |
73
|
|
|
/** @var CartItem $item */ |
74
|
91 |
|
foreach ($this->cart->getItems() as $item) { |
75
|
|
|
$id = $item->getClassId(); |
76
|
|
|
$class = $item->getClassName(); |
77
|
|
|
$entity = $this->em->getRepository($class)->find($id); |
78
|
|
|
$item->setObject($entity); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
24 |
|
public function addProduct($ProductClass, $quantity = 1) |
|
|
|
|
83
|
|
|
{ |
84
|
24 |
|
if (!$ProductClass instanceof ProductClass) { |
85
|
24 |
|
$ProductClassId = $ProductClass; |
86
|
24 |
|
$ProductClass = $this->em |
87
|
24 |
|
->getRepository(ProductClass::class) |
88
|
24 |
|
->find($ProductClassId); |
89
|
24 |
|
if (is_null($ProductClass)) { |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** @var Cart $cart */ |
95
|
24 |
|
$cart = $this->getCart(); |
96
|
24 |
|
$exists = $cart->getCartItemByIdentifier(ProductClass::class, $ProductClass->getId()); |
97
|
|
|
|
98
|
24 |
|
if ($exists) { |
99
|
3 |
|
$exists->setQuantity($exists->getQuantity() + $quantity); |
100
|
|
|
} else { |
101
|
24 |
|
$item = new CartItem(); |
102
|
24 |
|
$item->setQuantity($quantity); |
103
|
24 |
|
$item->setPrice($ProductClass->getPrice01IncTax()); |
104
|
24 |
|
$item->setClassId($ProductClass->getId()); |
105
|
24 |
|
$item->setClassName(ProductClass::class); |
106
|
24 |
|
$item->setObject($ProductClass); |
107
|
24 |
|
$cart->addItem($item); |
108
|
|
|
} |
109
|
|
|
|
110
|
24 |
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $cart_no |
115
|
|
|
* @param int $quantity |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function addQuantity($cart_no, $quantity = 1) |
119
|
|
|
{ |
120
|
|
|
$CartItem = $this->getCart()->getCartItemByCartNo($cart_no); |
121
|
|
|
|
122
|
|
|
if ($CartItem) { |
123
|
|
|
$CartItem->setQuantity($CartItem->getQuantity() + $quantity); |
124
|
|
|
return true; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param integer $cart_no |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
1 |
|
public function removeProduct($cart_no) |
135
|
|
|
{ |
136
|
1 |
|
$this->getCart()->removeCartItemByCartNo($cart_no); |
137
|
1 |
|
return true; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
|
|
|
|
141
|
|
|
* @param $cart_no |
|
|
|
|
142
|
|
|
* @return CartItem|null |
143
|
|
|
*/ |
144
|
3 |
|
public function getCartItem($cart_no) |
145
|
|
|
{ |
146
|
3 |
|
return $this->getCart()->getCartItemByCartNo($cart_no); |
147
|
|
|
} |
148
|
|
|
|
149
|
33 |
|
public function save() |
|
|
|
|
150
|
|
|
{ |
151
|
33 |
|
return $this->session->set('cart', $this->getCart()); |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
public function unlock() |
|
|
|
|
155
|
|
|
{ |
156
|
3 |
|
$this->getCart() |
157
|
3 |
|
->setLock(false) |
158
|
3 |
|
->setPreOrderId(null); |
159
|
|
|
} |
160
|
|
|
|
161
|
15 |
|
public function lock() |
|
|
|
|
162
|
|
|
{ |
163
|
15 |
|
$this->getCart() |
164
|
15 |
|
->setLock(true) |
165
|
15 |
|
->setPreOrderId(null); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
22 |
|
public function isLocked() |
172
|
|
|
{ |
173
|
22 |
|
return $this->getCart()->getLock(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $pre_order_id |
178
|
|
|
* @return \Eccube\Service\CartService |
179
|
|
|
*/ |
180
|
12 |
|
public function setPreOrderId($pre_order_id) |
181
|
|
|
{ |
182
|
12 |
|
$this->getCart()->setPreOrderId($pre_order_id); |
183
|
|
|
|
184
|
12 |
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
12 |
|
public function getPreOrderId() |
191
|
|
|
{ |
192
|
12 |
|
return $this->getCart()->getPreOrderId(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return \Eccube\Service\CartService |
197
|
|
|
*/ |
198
|
24 |
|
public function clear() |
199
|
|
|
{ |
200
|
24 |
|
$this->getCart() |
201
|
24 |
|
->setPreOrderId(null) |
202
|
24 |
|
->setLock(false) |
203
|
24 |
|
->clearCartItems(); |
204
|
|
|
|
205
|
24 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|