1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Checkout\Promotion\Cart\Discount; |
4
|
|
|
|
5
|
|
|
use Shopware\Core\Checkout\Cart\CartException; |
6
|
|
|
use Shopware\Core\Checkout\Cart\LineItem\Group\LineItemQuantityCollection; |
7
|
|
|
use Shopware\Core\Checkout\Cart\LineItem\LineItem; |
8
|
|
|
use Shopware\Core\Checkout\Cart\LineItem\LineItemFlatCollection; |
9
|
|
|
use Shopware\Core\Checkout\Cart\Price\Struct\PriceCollection; |
10
|
|
|
use Shopware\Core\Checkout\Promotion\Exception\PriceNotFoundException; |
11
|
|
|
use Shopware\Core\Framework\Log\Package; |
12
|
|
|
|
13
|
|
|
#[Package('checkout')] |
14
|
|
|
class DiscountPackage |
15
|
|
|
{ |
16
|
|
|
private LineItemFlatCollection $cartItems; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array<string, LineItem>|null |
20
|
|
|
*/ |
21
|
|
|
private ?array $hashMap; |
22
|
|
|
|
23
|
|
|
public function __construct(private LineItemQuantityCollection $metaItems) |
24
|
|
|
{ |
25
|
|
|
$this->cartItems = new LineItemFlatCollection(); |
26
|
|
|
$this->hashMap = null; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getMetaData(): LineItemQuantityCollection |
30
|
|
|
{ |
31
|
|
|
return $this->metaItems; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setMetaItems(LineItemQuantityCollection $metaItems): void |
35
|
|
|
{ |
36
|
|
|
$this->metaItems = $metaItems; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getCartItems(): LineItemFlatCollection |
40
|
|
|
{ |
41
|
|
|
return $this->cartItems; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getCartItem(string $id): LineItem |
45
|
|
|
{ |
46
|
|
|
$map = $this->hasMap(); |
47
|
|
|
|
48
|
|
|
if (isset($map[$id])) { |
49
|
|
|
return $map[$id]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
throw CartException::lineItemNotFound($id); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setCartItems(LineItemFlatCollection $items): void |
56
|
|
|
{ |
57
|
|
|
$this->cartItems = $items; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Gets the overall total price of all cart items in this package. |
62
|
|
|
*/ |
63
|
|
|
public function getTotalPrice(): float |
64
|
|
|
{ |
65
|
|
|
$price = 0; |
66
|
|
|
|
67
|
|
|
foreach ($this->cartItems as $item) { |
68
|
|
|
if ($item->getPrice() !== null) { |
69
|
|
|
$price += $item->getPrice()->getTotalPrice(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $price; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the price collection of all cart items in this package. |
78
|
|
|
* |
79
|
|
|
* @throws PriceNotFoundException |
80
|
|
|
*/ |
81
|
|
|
public function getAffectedPrices(): PriceCollection |
82
|
|
|
{ |
83
|
|
|
$affectedPrices = new PriceCollection(); |
84
|
|
|
|
85
|
|
|
foreach ($this->cartItems as $lineItem) { |
86
|
|
|
if ($lineItem->getPrice() === null) { |
87
|
|
|
throw new PriceNotFoundException($lineItem); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$affectedPrices->add($lineItem->getPrice()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $affectedPrices; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return array<string, LineItem> |
98
|
|
|
*/ |
99
|
|
|
private function hasMap(): array |
100
|
|
|
{ |
101
|
|
|
if ($this->hashMap !== null) { |
102
|
|
|
return $this->hashMap; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->hashMap = []; |
106
|
|
|
foreach ($this->cartItems as $item) { |
107
|
|
|
// previous implementation always took the first element which maps the id |
108
|
|
|
// to prevent side effects, we keep this logic |
109
|
|
|
if (isset($this->hashMap[$item->getId()])) { |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
$this->hashMap[$item->getId()] = $item; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->hashMap; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|