/
AddToCartForm.php
243 lines (214 loc) · 7.51 KB
/
AddToCartForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace Drupal\commerce_cart\Form;
use Drupal\commerce\Context;
use Drupal\commerce_cart\CartManagerInterface;
use Drupal\commerce_cart\CartProviderInterface;
use Drupal\commerce_order\Resolver\OrderTypeResolverInterface;
use Drupal\commerce_price\Resolver\ChainPriceResolverInterface;
use Drupal\commerce_store\CurrentStoreInterface;
use Drupal\commerce_store\SelectStoreTrait;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the order item add to cart form.
*/
class AddToCartForm extends ContentEntityForm implements AddToCartFormInterface {
use SelectStoreTrait;
/**
* The cart manager.
*
* @var \Drupal\commerce_cart\CartManagerInterface
*/
protected $cartManager;
/**
* The cart provider.
*
* @var \Drupal\commerce_cart\CartProviderInterface
*/
protected $cartProvider;
/**
* The order type resolver.
*
* @var \Drupal\commerce_order\Resolver\OrderTypeResolverInterface
*/
protected $orderTypeResolver;
/**
* The current store.
*
* @var \Drupal\commerce_store\CurrentStoreInterface
*/
protected $currentStore;
/**
* The chain base price resolver.
*
* @var \Drupal\commerce_price\Resolver\ChainPriceResolverInterface
*/
protected $chainPriceResolver;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The form ID.
*
* @var string
*/
protected $formId;
/**
* Constructs a new AddToCartForm object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time.
* @param \Drupal\commerce_cart\CartManagerInterface $cart_manager
* The cart manager.
* @param \Drupal\commerce_cart\CartProviderInterface $cart_provider
* The cart provider.
* @param \Drupal\commerce_order\Resolver\OrderTypeResolverInterface $order_type_resolver
* The order type resolver.
* @param \Drupal\commerce_store\CurrentStoreInterface $current_store
* The current store.
* @param \Drupal\commerce_price\Resolver\ChainPriceResolverInterface $chain_price_resolver
* The chain base price resolver.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, CartManagerInterface $cart_manager, CartProviderInterface $cart_provider, OrderTypeResolverInterface $order_type_resolver, CurrentStoreInterface $current_store, ChainPriceResolverInterface $chain_price_resolver, AccountInterface $current_user) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->cartManager = $cart_manager;
$this->cartProvider = $cart_provider;
$this->orderTypeResolver = $order_type_resolver;
$this->currentStore = $current_store;
$this->chainPriceResolver = $chain_price_resolver;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.repository'),
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('commerce_cart.cart_manager'),
$container->get('commerce_cart.cart_provider'),
$container->get('commerce_order.chain_order_type_resolver'),
$container->get('commerce_store.current_store'),
$container->get('commerce_price.chain_price_resolver'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function setEntity(EntityInterface $entity) {
$this->entity = $entity;
return $this;
}
/**
* {@inheritdoc}
*/
public function getBaseFormId() {
return $this->entity->getEntityTypeId() . '_' . $this->operation . '_form';
}
/**
* {@inheritdoc}
*/
public function getFormId() {
if (empty($this->formId)) {
$this->formId = $this->getBaseFormId();
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$order_item = $this->entity;
if ($purchased_entity = $order_item->getPurchasedEntity()) {
$this->formId .= '_' . $purchased_entity->getEntityTypeId() . '_' . $purchased_entity->id();
}
}
return $this->formId;
}
/**
* {@inheritdoc}
*/
public function setFormId($form_id) {
$this->formId = $form_id;
return $this;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
// The widgets are allowed to signal that the form should be hidden
// (because there's no purchasable entity to select, for example).
if ($form_state->get('hide_form')) {
$form['#access'] = FALSE;
}
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Add to cart'),
'#submit' => ['::submitForm'],
'#attributes' => [
'class' => ['button--add-to-cart'],
],
];
return $actions;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$order_item = $this->entity;
/** @var \Drupal\commerce\PurchasableEntityInterface $purchased_entity */
$purchased_entity = $order_item->getPurchasedEntity();
$cart = $order_item->getOrder();
if (!$cart) {
$order_type_id = $this->orderTypeResolver->resolve($order_item);
$store = $this->selectStore($purchased_entity);
$cart = $this->cartProvider->createCart($order_type_id, $store);
}
$this->entity = $this->cartManager->addOrderItem($cart, $order_item, $form_state->get(['settings', 'combine']));
// Other submit handlers might need the cart ID.
$form_state->set('cart_id', $cart->id());
}
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $entity */
$entity = parent::buildEntity($form, $form_state);
// Now that the purchased entity is set, populate the title and price.
$purchased_entity = $entity->getPurchasedEntity();
$entity->setTitle($purchased_entity->getOrderItemTitle());
if (!$entity->isUnitPriceOverridden()) {
$store = $this->selectStore($purchased_entity);
$context = new Context($this->currentUser, $store);
$resolved_price = $this->chainPriceResolver->resolve($purchased_entity, $entity->getQuantity(), $context);
$entity->setUnitPrice($resolved_price);
}
$order_type_id = $this->orderTypeResolver->resolve($entity);
$store = $this->selectStore($purchased_entity);
$cart = $this->cartProvider->getCart($order_type_id, $store);
if ($cart) {
$entity->set('order_id', $cart->id());
}
return $entity;
}
}