1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class DiscountCouponProductDataExtension extends DataExtension |
|
|
|
|
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* stadard SS declaration |
10
|
|
|
* @var Array |
11
|
|
|
*/ |
12
|
|
|
private static $belongs_many_many = array( |
|
|
|
|
13
|
|
|
"ApplicableDiscountCoupons" => "DiscountCouponOption" |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Update Fields |
19
|
|
|
* @return FieldList |
20
|
|
|
*/ |
21
|
|
|
public function updateCMSFields(FieldList $fields) |
22
|
|
|
{ |
23
|
|
|
$fields->addFieldsToTab( |
24
|
|
|
'Root.Discount', |
25
|
|
|
GridField::create( |
|
|
|
|
26
|
|
|
'ApplicableDiscountCoupons', |
27
|
|
|
'Discount Coupons', |
28
|
|
|
$this->owner->ApplicableDiscountCoupons(), |
29
|
|
|
GridFieldConfig_RelationEditor::create() |
30
|
|
|
) |
31
|
|
|
); |
32
|
|
|
return $fields; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param float $price |
|
|
|
|
37
|
|
|
* |
38
|
|
|
* @return float | null |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
public function updateCalculatedPrice($price = null) |
41
|
|
|
{ |
42
|
|
|
$hasDiscount = false; |
43
|
|
|
$coupons = $this->owner->DirectlyApplicableDiscountCoupons(); |
44
|
|
|
if ($coupons && $coupons->count()) { |
45
|
|
|
$discountPercentage = 0; |
46
|
|
|
$discountAbsolute = 0; |
47
|
|
|
foreach ($coupons as $coupon) { |
48
|
|
|
if ($coupon->isValid()) { |
49
|
|
|
$hasDiscount = true; |
50
|
|
|
if ($coupon->DiscountPercentage > $discountPercentage) { |
51
|
|
|
$discountPercentage = $coupon->DiscountPercentage; |
52
|
|
|
} |
53
|
|
|
if ($coupon->DiscountAbsolute > $discountAbsolute) { |
54
|
|
|
$discountAbsolute = $coupon->DiscountAbsolute; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
if ($hasDiscount) { |
59
|
|
|
$priceWithPercentageDiscount = $price - ($price * ($discountPercentage / 100)); |
60
|
|
|
$priceWithAbsoluteDiscount = $price - $discountAbsolute; |
61
|
|
|
if ($priceWithPercentageDiscount < $priceWithAbsoluteDiscount) { |
62
|
|
|
return $priceWithPercentageDiscount; |
63
|
|
|
} else { |
64
|
|
|
return $priceWithAbsoluteDiscount; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function DirectlyApplicableDiscountCoupons() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return $this->owner->ApplicableDiscountCoupons() |
73
|
|
|
->filter(array("ApplyPercentageToApplicableProducts" => 1, "ApplyEvenWithoutCode" => 1)); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* @return SS_Date |
|
|
|
|
79
|
|
|
*/ |
80
|
|
|
public function DiscountsAvailableUntil() |
81
|
|
|
{ |
82
|
|
|
$coupons = $this->DirectlyApplicableDiscountCoupons(); |
83
|
|
|
$next = strtotime('+100 years'); |
84
|
|
|
if ($coupons && $coupons->count()) { |
85
|
|
|
$discount = 0; |
86
|
|
|
foreach ($coupons as $coupon) { |
87
|
|
|
if ($coupon->isValid()) { |
88
|
|
|
if ($coupon->EndDate && $coupon->DiscountAbsolute > $discount) { |
89
|
|
|
$discount = $coupon->DiscountAbsolute; |
90
|
|
|
$maxDate = strtotime($coupon->EndDate); |
91
|
|
|
if ($maxDate < $next) { |
92
|
|
|
$next = $maxDate; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
if ($next) { |
99
|
|
|
return DBField::create_field('Date', $next); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.