1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Checkout\Test\Cart\Promotion\Helpers\Traits; |
4
|
|
|
|
5
|
|
|
use Shopware\Core\Checkout\Promotion\Aggregate\PromotionDiscount\PromotionDiscountEntity; |
6
|
|
|
use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition; |
7
|
|
|
use Shopware\Core\Defaults; |
8
|
|
|
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; |
9
|
|
|
use Shopware\Core\Framework\Test\TestCaseBase\TaxAddToSalesChannelTestBehaviour; |
10
|
|
|
use Shopware\Core\Framework\Uuid\Uuid; |
11
|
|
|
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory; |
12
|
|
|
use Shopware\Core\System\SalesChannel\SalesChannelContext; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
|
15
|
|
|
trait PromotionTestFixtureBehaviour |
16
|
|
|
{ |
17
|
|
|
use TaxAddToSalesChannelTestBehaviour; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param $value |
21
|
|
|
*/ |
22
|
|
|
public function createSetGroupFixture(string $packagerKey, $value, string $sorterKey, string $promotionId, ContainerInterface $container): string |
23
|
|
|
{ |
24
|
|
|
$context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL); |
25
|
|
|
|
26
|
|
|
$repository = $container->get('promotion_setgroup.repository'); |
27
|
|
|
|
28
|
|
|
$groupId = Uuid::randomHex(); |
29
|
|
|
|
30
|
|
|
$data = [ |
31
|
|
|
'id' => $groupId, |
32
|
|
|
'promotionId' => $promotionId, |
33
|
|
|
'packagerKey' => $packagerKey, |
34
|
|
|
'sorterKey' => $sorterKey, |
35
|
|
|
'value' => $value, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
$repository->create([$data], $context->getContext()); |
39
|
|
|
|
40
|
|
|
return $groupId; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a new product in the database. |
45
|
|
|
*/ |
46
|
|
|
private function createTestFixtureProduct(string $productId, float $grossPrice, float $taxRate, ContainerInterface $container, SalesChannelContext $context): void |
47
|
|
|
{ |
48
|
|
|
/** @var EntityRepositoryInterface $productRepository */ |
49
|
|
|
$productRepository = $container->get('product.repository'); |
50
|
|
|
|
51
|
|
|
$tax = ['id' => Uuid::randomHex(), 'taxRate' => $taxRate, 'name' => 'with id']; |
52
|
|
|
|
53
|
|
|
$productRepository->create( |
54
|
|
|
[ |
55
|
|
|
[ |
56
|
|
|
'id' => $productId, |
57
|
|
|
'productNumber' => $productId, |
58
|
|
|
'stock' => 1, |
59
|
|
|
'name' => 'Test', |
60
|
|
|
'active' => true, |
61
|
|
|
'price' => [ |
62
|
|
|
[ |
63
|
|
|
'currencyId' => Defaults::CURRENCY, |
64
|
|
|
'gross' => $grossPrice, |
65
|
|
|
'net' => 9, 'linked' => false, |
66
|
|
|
], |
67
|
|
|
], |
68
|
|
|
'manufacturer' => ['name' => 'test'], |
69
|
|
|
'tax' => $tax, |
70
|
|
|
'visibilities' => [ |
71
|
|
|
['salesChannelId' => $context->getSalesChannel()->getId(), 'visibility' => ProductVisibilityDefinition::VISIBILITY_ALL], |
72
|
|
|
], |
73
|
|
|
'categories' => [ |
74
|
|
|
['id' => Uuid::randomHex(), 'name' => 'Clothing'], |
75
|
|
|
], |
76
|
|
|
], |
77
|
|
|
], |
78
|
|
|
$context->getContext() |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
$this->addTaxDataToSalesChannel($context, $tax); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates a new absolute promotion in the database. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
private function createTestFixtureAbsolutePromotion(string $promotionId, string $code, float $value, ContainerInterface $container, string $scope = PromotionDiscountEntity::SCOPE_CART) |
90
|
|
|
{ |
91
|
|
|
/** @var EntityRepositoryInterface $promotionRepository */ |
92
|
|
|
$promotionRepository = $container->get('promotion.repository'); |
93
|
|
|
|
94
|
|
|
$context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL); |
95
|
|
|
|
96
|
|
|
$this->createPromotion( |
97
|
|
|
$promotionId, |
98
|
|
|
$code, |
99
|
|
|
$promotionRepository, |
100
|
|
|
$context |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return $this->createTestFixtureDiscount($promotionId, PromotionDiscountEntity::TYPE_ABSOLUTE, $scope, $value, null, $container, $context); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Creates a new percentage promotion in the database. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
private function createTestFixturePercentagePromotion(string $promotionId, ?string $code, float $percentage, ?float $maxValue, ContainerInterface $container, string $scope = PromotionDiscountEntity::SCOPE_CART) |
112
|
|
|
{ |
113
|
|
|
/** @var EntityRepositoryInterface $promotionRepository */ |
114
|
|
|
$promotionRepository = $container->get('promotion.repository'); |
115
|
|
|
|
116
|
|
|
$context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL); |
117
|
|
|
|
118
|
|
|
$this->createPromotion( |
119
|
|
|
$promotionId, |
120
|
|
|
$code, |
121
|
|
|
$promotionRepository, |
122
|
|
|
$context |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $this->createTestFixtureDiscount($promotionId, PromotionDiscountEntity::TYPE_PERCENTAGE, $scope, $percentage, $maxValue, $container, $context); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Creates a new percentage promotion in the database. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
private function createTestFixtureSetGroupPromotion(string $promotionId, ?string $code, ContainerInterface $container) |
134
|
|
|
{ |
135
|
|
|
/** @var EntityRepositoryInterface $promotionRepository */ |
136
|
|
|
$promotionRepository = $container->get('promotion.repository'); |
137
|
|
|
|
138
|
|
|
$context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL); |
139
|
|
|
|
140
|
|
|
$this->createSetGroupPromotion( |
141
|
|
|
$promotionId, |
142
|
|
|
$code, |
143
|
|
|
$promotionRepository, |
144
|
|
|
$context |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function createSetGroupDiscount( |
149
|
|
|
string $promotionId, |
150
|
|
|
int $groupIndex, |
151
|
|
|
ContainerInterface $container, |
152
|
|
|
float $value, |
153
|
|
|
?float $maxValue, |
154
|
|
|
string $discountType = PromotionDiscountEntity::TYPE_PERCENTAGE, |
155
|
|
|
string $sortKey = 'PRICE_ASC', |
156
|
|
|
string $applierKey = 'ALL', |
157
|
|
|
string $usageKey = 'ALL', |
158
|
|
|
string $pickerKey = 'VERTICAL' |
159
|
|
|
) { |
160
|
|
|
$scope = PromotionDiscountEntity::SCOPE_SETGROUP . '-' . $groupIndex; |
161
|
|
|
|
162
|
|
|
$context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL); |
163
|
|
|
|
164
|
|
|
/** @var EntityRepositoryInterface $discountRepository */ |
165
|
|
|
$discountRepository = $container->get('promotion_discount.repository'); |
166
|
|
|
|
167
|
|
|
$discountId = Uuid::randomHex(); |
168
|
|
|
|
169
|
|
|
$data = [ |
170
|
|
|
'id' => $discountId, |
171
|
|
|
'promotionId' => $promotionId, |
172
|
|
|
'scope' => $scope, |
173
|
|
|
'type' => $discountType, |
174
|
|
|
'value' => $value, |
175
|
|
|
'considerAdvancedRules' => true, |
176
|
|
|
'sorterKey' => $sortKey, |
177
|
|
|
'applierKey' => $applierKey, |
178
|
|
|
'usageKey' => $usageKey, |
179
|
|
|
'pickerKey' => $pickerKey, |
180
|
|
|
]; |
181
|
|
|
|
182
|
|
|
if ($maxValue !== null) { |
183
|
|
|
$data['maxValue'] = $maxValue; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$discountRepository->create([$data], $context->getContext()); |
187
|
|
|
|
188
|
|
|
return $discountId; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Creates a new advanced currency price for the provided discount |
193
|
|
|
*/ |
194
|
|
|
private function createTestFixtureAdvancedPrice(string $discountId, string $currency, float $price, ContainerInterface $container): void |
195
|
|
|
{ |
196
|
|
|
/** @var EntityRepositoryInterface $pricesRepository */ |
197
|
|
|
$pricesRepository = $container->get('promotion_discount_prices.repository'); |
198
|
|
|
|
199
|
|
|
$context = $container->get(SalesChannelContextFactory::class)->create(Uuid::randomHex(), Defaults::SALES_CHANNEL); |
200
|
|
|
|
201
|
|
|
$pricesRepository->create( |
202
|
|
|
[ |
203
|
|
|
[ |
204
|
|
|
'discountId' => $discountId, |
205
|
|
|
'currencyId' => $currency, |
206
|
|
|
'price' => $price, |
207
|
|
|
], |
208
|
|
|
], |
209
|
|
|
$context->getContext() |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* function creates a discount for a promotion |
215
|
|
|
*/ |
216
|
|
|
private function createTestFixtureDiscount( |
217
|
|
|
string $promotionId, |
218
|
|
|
string $discountType, |
219
|
|
|
string $scope, |
220
|
|
|
float $value, |
221
|
|
|
?float $maxValue, |
222
|
|
|
ContainerInterface $container, |
223
|
|
|
SalesChannelContext $context, |
224
|
|
|
bool $considerAdvancedRules = false |
225
|
|
|
): string { |
226
|
|
|
$discountRepository = $container->get('promotion_discount.repository'); |
227
|
|
|
|
228
|
|
|
$discountId = Uuid::randomHex(); |
229
|
|
|
|
230
|
|
|
$data = [ |
231
|
|
|
'id' => $discountId, |
232
|
|
|
'promotionId' => $promotionId, |
233
|
|
|
'scope' => $scope, |
234
|
|
|
'type' => $discountType, |
235
|
|
|
'value' => $value, |
236
|
|
|
'considerAdvancedRules' => $considerAdvancedRules, |
237
|
|
|
]; |
238
|
|
|
|
239
|
|
|
if ($maxValue !== null) { |
240
|
|
|
$data['maxValue'] = $maxValue; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$discountRepository->create([$data], $context->getContext()); |
244
|
|
|
|
245
|
|
|
return $discountId; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* function creates a promotion |
250
|
|
|
*/ |
251
|
|
|
private function createPromotion(string $promotionId, ?string $code, EntityRepositoryInterface $promotionRepository, SalesChannelContext $context): void |
252
|
|
|
{ |
253
|
|
|
$data = [ |
254
|
|
|
'id' => $promotionId, |
255
|
|
|
'name' => 'Black Friday', |
256
|
|
|
'active' => true, |
257
|
|
|
'useCodes' => false, |
258
|
|
|
'useSetGroups' => false, |
259
|
|
|
'salesChannels' => [ |
260
|
|
|
['salesChannelId' => $context->getSalesChannel()->getId(), 'priority' => 1], |
261
|
|
|
], |
262
|
|
|
]; |
263
|
|
|
|
264
|
|
|
if ($code !== null) { |
265
|
|
|
$data['code'] = $code; |
266
|
|
|
$data['useCodes'] = true; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$promotionRepository->create([$data], $context->getContext()); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
private function createSetGroupPromotion(string $promotionId, ?string $code, EntityRepositoryInterface $promotionRepository, SalesChannelContext $context): void |
273
|
|
|
{ |
274
|
|
|
$data = [ |
275
|
|
|
'id' => $promotionId, |
276
|
|
|
'name' => 'Black Friday', |
277
|
|
|
'active' => true, |
278
|
|
|
'useCodes' => false, |
279
|
|
|
'useSetGroups' => true, |
280
|
|
|
'salesChannels' => [ |
281
|
|
|
['salesChannelId' => $context->getSalesChannel()->getId(), 'priority' => 1], |
282
|
|
|
], |
283
|
|
|
]; |
284
|
|
|
|
285
|
|
|
if ($code !== null) { |
286
|
|
|
$data['code'] = $code; |
287
|
|
|
$data['useCodes'] = true; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$promotionRepository->create([$data], $context->getContext()); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Creates a new promotion with a discount that has a scope DELIVERY |
295
|
|
|
*/ |
296
|
|
|
private function createTestFixtureDeliveryPromotion( |
297
|
|
|
string $promotionId, |
298
|
|
|
string $discountType, |
299
|
|
|
float $value, |
300
|
|
|
ContainerInterface $container, |
301
|
|
|
SalesChannelContext $context, |
302
|
|
|
?string $code |
303
|
|
|
): string { |
304
|
|
|
/** @var EntityRepositoryInterface $promotionRepository */ |
305
|
|
|
$promotionRepository = $container->get('promotion.repository'); |
306
|
|
|
|
307
|
|
|
$this->createPromotion( |
308
|
|
|
$promotionId, |
309
|
|
|
$code, |
310
|
|
|
$promotionRepository, |
311
|
|
|
$context |
312
|
|
|
); |
313
|
|
|
|
314
|
|
|
$deliveryId = $this->createTestFixtureDiscount( |
315
|
|
|
$promotionId, |
316
|
|
|
$discountType, |
317
|
|
|
PromotionDiscountEntity::SCOPE_DELIVERY, |
318
|
|
|
$value, |
319
|
|
|
null, |
320
|
|
|
$container, |
321
|
|
|
$context |
322
|
|
|
); |
323
|
|
|
|
324
|
|
|
return $deliveryId; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* function creates a promotion and a discount for it. |
329
|
|
|
* function returns the id of the new discount |
330
|
|
|
*/ |
331
|
|
|
private function createTestFixtureFixedUnitDiscountPromotion( |
332
|
|
|
string $promotionId, |
333
|
|
|
float $fixedPrice, |
334
|
|
|
string $scope, |
335
|
|
|
?string $code, |
336
|
|
|
ContainerInterface $container, |
337
|
|
|
SalesChannelContext $context, |
338
|
|
|
bool $considerAdvancedRules = false |
339
|
|
|
): string { |
340
|
|
|
/** @var EntityRepositoryInterface $promotionRepository */ |
341
|
|
|
$promotionRepository = $container->get('promotion.repository'); |
342
|
|
|
|
343
|
|
|
$this->createPromotion( |
344
|
|
|
$promotionId, |
345
|
|
|
$code, |
346
|
|
|
$promotionRepository, |
347
|
|
|
$context |
348
|
|
|
); |
349
|
|
|
|
350
|
|
|
$discountId = $this->createTestFixtureDiscount( |
351
|
|
|
$promotionId, |
352
|
|
|
PromotionDiscountEntity::TYPE_FIXED_UNIT, |
353
|
|
|
$scope, |
354
|
|
|
$fixedPrice, |
355
|
|
|
null, |
356
|
|
|
$this->getContainer(), |
|
|
|
|
357
|
|
|
$context, |
358
|
|
|
$considerAdvancedRules |
359
|
|
|
); |
360
|
|
|
|
361
|
|
|
return $discountId; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* function creates a promotion and a discount for it. |
366
|
|
|
* function returns the id of the new discount |
367
|
|
|
*/ |
368
|
|
|
private function createTestFixtureFixedDiscountPromotion(string $promotionId, float $fixedPrice, string $scope, ?string $code, ContainerInterface $container, SalesChannelContext $context): string |
369
|
|
|
{ |
370
|
|
|
/** @var EntityRepositoryInterface $promotionRepository */ |
371
|
|
|
$promotionRepository = $container->get('promotion.repository'); |
372
|
|
|
|
373
|
|
|
$this->createPromotion( |
374
|
|
|
$promotionId, |
375
|
|
|
$code, |
376
|
|
|
$promotionRepository, |
377
|
|
|
$context |
378
|
|
|
); |
379
|
|
|
|
380
|
|
|
$discountId = $this->createTestFixtureDiscount( |
381
|
|
|
$promotionId, |
382
|
|
|
PromotionDiscountEntity::TYPE_FIXED, |
383
|
|
|
$scope, |
384
|
|
|
$fixedPrice, |
385
|
|
|
null, |
386
|
|
|
$this->getContainer(), |
387
|
|
|
$context, |
388
|
|
|
false |
389
|
|
|
); |
390
|
|
|
|
391
|
|
|
return $discountId; |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|