1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discounts; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Discounts\Model\Discount; |
6
|
|
|
use Dynamic\Foxy\Model\ProductOption; |
7
|
|
|
use SilverStripe\Core\Injector\Injectable; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class DiscountHelper |
11
|
|
|
* @package Dynamic\Foxy\Discounts |
12
|
|
|
*/ |
13
|
|
|
class DiscountHelper |
14
|
|
|
{ |
15
|
|
|
use Injectable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
private $product; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ProductOption |
24
|
|
|
*/ |
25
|
|
|
private $product_option; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Discount |
29
|
|
|
*/ |
30
|
|
|
private $discount; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var |
34
|
|
|
*/ |
35
|
|
|
private $discounted_price; |
36
|
|
|
|
37
|
|
|
public function __construct($product, $discount, $productOption = null) |
38
|
|
|
{ |
39
|
|
|
$this->setProduct($product); |
40
|
|
|
$this->setDiscount($discount); |
41
|
|
|
|
42
|
|
|
if ($productOption instanceof ProductOption || is_string($productOption)) { |
43
|
|
|
$this->setProductOption($productOption); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->setDiscountedPrice(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function getProduct() |
53
|
|
|
{ |
54
|
|
|
return $this->product; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $product |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function setProduct($product): self |
62
|
|
|
{ |
63
|
|
|
$this->product = $product; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function getProductOption() |
72
|
|
|
{ |
73
|
|
|
return $this->product_option; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ProductOption $productOption |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setProductOption($productOption): self |
81
|
|
|
{ |
82
|
|
|
$this->product_option = ($productOption instanceof ProductOption) |
|
|
|
|
83
|
|
|
? $productOption |
84
|
|
|
: $this->getProduct()->Options()->filter('OptionModifierKey', $productOption)->first(); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
public function getDiscount() |
93
|
|
|
{ |
94
|
|
|
return $this->discount; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Discount $discount |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setDiscount(Discount $discount): self |
102
|
|
|
{ |
103
|
|
|
$this->discount = $discount; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return float|int |
110
|
|
|
*/ |
111
|
|
|
public function setDiscountedPrice() |
112
|
|
|
{ |
113
|
|
|
$price = ($this->getProductOption()) |
114
|
|
|
? $this->getProductOption()->getPrice($this->getProduct()) |
115
|
|
|
: $this->getProduct()->Price; |
116
|
|
|
|
117
|
|
|
$tier = $this->getDiscount()->DiscountTiers() |
|
|
|
|
118
|
|
|
->filter('Quantity:LessThanOrEqual', 1) |
119
|
|
|
->sort('Quantity DESC')->first(); |
120
|
|
|
|
121
|
|
|
$price = ($this->getDiscount()->Type == 'Percent') |
|
|
|
|
122
|
|
|
? $price - ($price * ($tier->Percentage/100)) |
123
|
|
|
: $price - $tier->Amount; |
124
|
|
|
|
125
|
|
|
return $this->discounted_price = $price; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return mixed |
130
|
|
|
*/ |
131
|
|
|
public function getDiscountedPrice() |
132
|
|
|
{ |
133
|
|
|
if (!$this->discounted_price) { |
134
|
|
|
$this->setDiscountedPrice(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->discounted_price; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|