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
|
|
|
protected static $buyable_to_be_excluded_from_discounts = []; |
36
|
|
|
|
37
|
|
|
public static function add_buyable_to_be_excluded($buyableOrBuyableID) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if(is_object($buyable)) { |
|
|
|
|
40
|
|
|
$id = $buyable->ID; |
|
|
|
|
41
|
|
|
} elseif(intval($buyable)) { |
|
|
|
|
42
|
|
|
$id = intval($buyable); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
self::$buyable_to_be_excluded_from_discounts[$id] = $id; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setCanBeNotDiscounted() |
49
|
|
|
{ |
50
|
|
|
self::$buyable_to_be_excluded_from_discounts[$this->owner->ID] = $this->owner->ID; |
|
|
|
|
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getCanBeDiscounted() |
56
|
|
|
{ |
57
|
|
|
return isset(self::$buyable_to_be_excluded_from_discounts[$this->owner->ID]) ? false : true; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param float $price |
|
|
|
|
62
|
|
|
* |
63
|
|
|
* @return float | null |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public function updateCalculatedPrice($price = null) |
66
|
|
|
{ |
67
|
|
|
if($this->getCanBeDiscounted()) { |
68
|
|
|
$hasDiscount = false; |
69
|
|
|
$coupons = $this->owner->DirectlyApplicableDiscountCoupons(); |
70
|
|
|
if ($coupons && $coupons->count()) { |
71
|
|
|
$discountPercentage = 0; |
72
|
|
|
$discountAbsolute = 0; |
73
|
|
|
foreach ($coupons as $coupon) { |
74
|
|
|
if ($coupon->isValid()) { |
75
|
|
|
$hasDiscount = true; |
76
|
|
|
if ($coupon->DiscountPercentage > $discountPercentage) { |
77
|
|
|
$discountPercentage = $coupon->DiscountPercentage; |
78
|
|
|
} |
79
|
|
|
if ($coupon->DiscountAbsolute > $discountAbsolute) { |
80
|
|
|
$discountAbsolute = $coupon->DiscountAbsolute; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
if ($hasDiscount) { |
85
|
|
|
$priceWithPercentageDiscount = $price - ($price * ($discountPercentage / 100)); |
86
|
|
|
$priceWithAbsoluteDiscount = $price - $discountAbsolute; |
87
|
|
|
if ($priceWithPercentageDiscount < $priceWithAbsoluteDiscount) { |
88
|
|
|
return $priceWithPercentageDiscount; |
89
|
|
|
} else { |
90
|
|
|
return $priceWithAbsoluteDiscount; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function DirectlyApplicableDiscountCoupons() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
return $this->owner->ApplicableDiscountCoupons() |
100
|
|
|
->filter(array("ApplyPercentageToApplicableProducts" => 1, "ApplyEvenWithoutCode" => 1)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* @return SS_Date |
|
|
|
|
106
|
|
|
*/ |
107
|
|
|
public function DiscountsAvailableUntil() |
108
|
|
|
{ |
109
|
|
|
$coupons = $this->DirectlyApplicableDiscountCoupons(); |
110
|
|
|
$next = strtotime('+100 years'); |
111
|
|
|
if ($coupons && $coupons->count()) { |
112
|
|
|
$discount = 0; |
113
|
|
|
foreach ($coupons as $coupon) { |
114
|
|
|
if ($coupon->isValid()) { |
115
|
|
|
if ($coupon->EndDate && $coupon->DiscountAbsolute > $discount) { |
116
|
|
|
$discount = $coupon->DiscountAbsolute; |
117
|
|
|
$maxDate = strtotime($coupon->EndDate); |
118
|
|
|
if ($maxDate < $next) { |
119
|
|
|
$next = $maxDate; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
if ($next) { |
126
|
|
|
return DBField::create_field('Date', $next); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: