|
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
|
|
|
private $discountCouponAmount = null; |
|
104
|
|
|
|
|
105
|
|
|
public function DiscountCouponAmount() |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->discountCouponAmount === null) { |
|
108
|
|
|
$this->discountCouponAmount = 0; |
|
109
|
|
|
$amount = floatval($this->owner->Price) - floatval($this->owner->CalculatedPrice()); |
|
|
|
|
|
|
110
|
|
|
if($amount > 1) { |
|
111
|
|
|
$this->discountCouponAmount = $amount; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
return EcommerceCurrency::get_money_object_from_order_currency($this->discountCouponAmount); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* |
|
119
|
|
|
* @return SS_Date |
|
|
|
|
|
|
120
|
|
|
*/ |
|
121
|
|
|
public function DiscountsAvailableUntil() |
|
122
|
|
|
{ |
|
123
|
|
|
$coupons = $this->DirectlyApplicableDiscountCoupons(); |
|
124
|
|
|
$next = strtotime('+100 years'); |
|
125
|
|
|
if ($coupons && $coupons->count()) { |
|
126
|
|
|
$discount = 0; |
|
127
|
|
|
foreach ($coupons as $coupon) { |
|
128
|
|
|
if ($coupon->isValid()) { |
|
129
|
|
|
if ($coupon->EndDate && $coupon->DiscountAbsolute > $discount) { |
|
130
|
|
|
$discount = $coupon->DiscountAbsolute; |
|
131
|
|
|
$maxDate = strtotime($coupon->EndDate); |
|
132
|
|
|
if ($maxDate < $next) { |
|
133
|
|
|
$next = $maxDate; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
if ($next) { |
|
140
|
|
|
return DBField::create_field('Date', $next); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
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: