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
|
39 |
|
public function addProduct($ProductClass, $quantity = 1) |
|
|
|
|
83
|
|
|
{ |
84
|
39 |
View Code Duplication |
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
|
39 |
|
$ClassCategory1 = $ProductClass->getClassCategory1(); |
94
|
39 |
|
if ($ClassCategory1 && !$ClassCategory1->isVisible()) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
39 |
|
$ClassCategory2 = $ProductClass->getClassCategory2(); |
98
|
39 |
|
if ($ClassCategory2 && !$ClassCategory2->isVisible()) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
/** @var Cart $cart */ |
102
|
39 |
|
$cart = $this->getCart(); |
103
|
39 |
|
$exists = $cart->getCartItemByIdentifier(ProductClass::class, $ProductClass->getId()); |
104
|
|
|
|
105
|
39 |
|
if ($exists) { |
106
|
3 |
|
$exists->setQuantity($exists->getQuantity() + $quantity); |
107
|
|
|
} else { |
108
|
39 |
|
$item = new CartItem(); |
109
|
39 |
|
$item->setQuantity($quantity); |
110
|
39 |
|
$item->setPrice($ProductClass->getPrice01IncTax()); |
111
|
39 |
|
$item->setClassId($ProductClass->getId()); |
112
|
39 |
|
$item->setClassName(ProductClass::class); |
113
|
39 |
|
$item->setObject($ProductClass); |
114
|
39 |
|
$cart->addItem($item); |
115
|
|
|
} |
116
|
|
|
|
117
|
39 |
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
2 |
|
public function removeProduct($ProductClass) |
|
|
|
|
121
|
|
|
{ |
122
|
2 |
View Code Duplication |
if (!$ProductClass instanceof ProductClass) { |
123
|
1 |
|
$ProductClassId = $ProductClass; |
124
|
1 |
|
$ProductClass = $this->em |
125
|
1 |
|
->getRepository(ProductClass::class) |
126
|
1 |
|
->find($ProductClassId); |
127
|
1 |
|
if (is_null($ProductClass)) { |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** @var Cart $cart */ |
133
|
2 |
|
$cart = $this->getCart(); |
134
|
2 |
|
$cart->removeCartItemByIdentifier(ProductClass::class, $ProductClass->getId()); |
135
|
|
|
|
136
|
2 |
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
36 |
|
public function save() |
|
|
|
|
140
|
|
|
{ |
141
|
36 |
|
return $this->session->set('cart', $this->getCart()); |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
public function unlock() |
|
|
|
|
145
|
|
|
{ |
146
|
3 |
|
$this->getCart() |
147
|
3 |
|
->setLock(false) |
148
|
3 |
|
->setPreOrderId(null); |
149
|
|
|
} |
150
|
|
|
|
151
|
15 |
|
public function lock() |
|
|
|
|
152
|
|
|
{ |
153
|
15 |
|
$this->getCart() |
154
|
15 |
|
->setLock(true) |
155
|
15 |
|
->setPreOrderId(null); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
22 |
|
public function isLocked() |
162
|
|
|
{ |
163
|
22 |
|
return $this->getCart()->getLock(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $pre_order_id |
168
|
|
|
* @return \Eccube\Service\CartService |
169
|
|
|
*/ |
170
|
12 |
|
public function setPreOrderId($pre_order_id) |
171
|
|
|
{ |
172
|
12 |
|
$this->getCart()->setPreOrderId($pre_order_id); |
173
|
|
|
|
174
|
12 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
12 |
|
public function getPreOrderId() |
181
|
|
|
{ |
182
|
12 |
|
return $this->getCart()->getPreOrderId(); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return \Eccube\Service\CartService |
187
|
|
|
*/ |
188
|
27 |
|
public function clear() |
189
|
|
|
{ |
190
|
27 |
|
$this->getCart() |
191
|
27 |
|
->setPreOrderId(null) |
192
|
27 |
|
->setLock(false) |
193
|
27 |
|
->clearCartItems(); |
194
|
|
|
|
195
|
27 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|