1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class ComplexPriceBuyableDecorator extends DataExtension |
|
|
|
|
5
|
|
|
{ |
6
|
|
|
private static $has_many = array( |
|
|
|
|
7
|
|
|
'ComplexPriceObjects' => 'ComplexPriceObject' |
8
|
|
|
); |
9
|
|
|
|
10
|
|
|
public function updateCMSFields(FieldList $fields) |
11
|
|
|
{ |
12
|
|
|
$tabName = 'Root.Pricing'; |
13
|
|
|
|
14
|
|
|
if (class_exists("DataObjectOneFieldUpdateController")) { |
15
|
|
|
$link = DataObjectOneFieldUpdateController::popup_link( |
16
|
|
|
$this->owner->ClassName, |
17
|
|
|
"Price", |
18
|
|
|
$where = "", |
19
|
|
|
$sort = "Price ASC ", |
20
|
|
|
$linkText = "Check all prices..." |
21
|
|
|
); |
22
|
|
|
$fields->AddFieldToTab($tabName, new HeaderField("MetaTitleFixesHeader", "Quick review", 3)); |
23
|
|
|
$fields->AddFieldToTab($tabName, new LiteralField("MetaTitleFixes", $link.".<br /><br /><br />")); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// move price field under new 'Pricing' tab |
|
|
|
|
27
|
|
|
// $priceField = $fields->fieldByName('Root.Details.Price'); |
28
|
|
|
// $fields->remove($priceField); |
29
|
|
|
|
30
|
|
|
$fields->addFieldsToTab( |
31
|
|
|
$tabName, |
32
|
|
|
array( |
33
|
|
|
// $priceField, |
34
|
|
|
new HeaderField("ComplexPricesHeader", "Alternative Pricing", 3), |
35
|
|
|
new LiteralField("ComplexPricesExplanation", "<p>Please enter <i>alternative</i> pricing below. You can enter a price per <a href=\"admin/security/\">security group</a> and/or per country.</p>"), |
36
|
|
|
$this->complexPricesHasManyTable() |
37
|
|
|
) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function complexPricesHasManyTable() |
42
|
|
|
{ |
43
|
|
|
$gridCfg = new GridFieldConfig_RelationEditor(); |
44
|
|
|
$grid = new GridField('ComplexPriceObjects', '', $this->owner->ComplexPriceObjects(), $gridCfg); |
45
|
|
|
return $grid; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function HasDiscount() |
49
|
|
|
{ |
50
|
|
|
if ($this->owner->Price > 0) { |
51
|
|
|
if ($this->owner->Price > $this->owner->getCalculatedPrice()) { |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function updateCalculatedPrice(&$startingPrice) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$newPrice = -1; |
61
|
|
|
$fieldName = $this->owner->ClassName."ID"; |
62
|
|
|
$singleton = ComplexPriceObject::get()->first(); |
63
|
|
|
if ($singleton) { |
64
|
|
|
// Check that ComplexPriceObject can be joined to this type of object |
65
|
|
|
if (!$singleton->hasField($fieldName)) { |
66
|
|
|
$ancestorArray = ClassInfo::ancestry($this->owner, true); |
|
|
|
|
67
|
|
|
foreach ($ancestorArray as $ancestor) { |
68
|
|
|
$fieldName = $ancestor."ID"; |
69
|
|
|
if ($singleton->hasField($fieldName)) { |
70
|
|
|
break; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Load up the alternate prices for this product |
76
|
|
|
$prices = ComplexPriceObject::get() |
77
|
|
|
->filter(array($fieldName => $this->owner->ID, "NoLongerValid" => 0)) |
78
|
|
|
->sort("NewPrice", "DESC"); |
79
|
|
|
; |
80
|
|
|
$memberGroupsArray = array(); |
81
|
|
|
if ($prices->count()) { |
82
|
|
|
// Load up the groups for the current memeber, if any |
83
|
|
|
if ($member = Member::currentUser()) { |
84
|
|
|
if ($memberGroupComponents = $member->getManyManyComponents('Groups')) { |
85
|
|
|
if ($memberGroupComponents && $memberGroupComponents->count()) { |
86
|
|
|
$memberGroupsArray = $memberGroupComponents->column("ID"); |
87
|
|
|
if (!is_array($memberGroupsArray)) { |
88
|
|
|
$memberGroupsArray = array(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$countryID = EcommerceCountry::get_country_id(); |
95
|
|
|
|
96
|
|
|
// Look at each price and see if it can be used |
97
|
|
|
foreach ($prices as $price) { |
98
|
|
|
$priceCanBeUsed = true; |
99
|
|
|
|
100
|
|
|
// Does it pass the group filter? |
101
|
|
|
if ($priceGroupComponents = $price->getManyManyComponents('Groups')) { |
102
|
|
|
if ($priceGroupComponents && $priceGroupComponents->count()) { |
103
|
|
|
$priceCanBeUsed = false; |
104
|
|
|
$priceGroupArray = $priceGroupComponents->column("ID"); |
105
|
|
|
if (!is_array($priceGroupArray)) { |
106
|
|
|
$priceGroupArray = array(); |
107
|
|
|
} |
108
|
|
|
$interSectionArray = array_intersect($priceGroupArray, $memberGroupsArray); |
109
|
|
|
if (is_array($interSectionArray) && count($interSectionArray)) { |
110
|
|
|
$priceCanBeUsed = true; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Does it pass the country filter? |
116
|
|
|
if ($priceCanBeUsed) { |
117
|
|
|
if ($priceCountryComponents = $price->getManyManyComponents('EcommerceCountries')) { |
118
|
|
|
if ($priceCountryComponents && $priceCountryComponents->count()) { |
119
|
|
|
$priceCanBeUsed = false; |
120
|
|
|
$priceCountryArray = $priceCountryComponents->column("ID"); |
121
|
|
|
if (!is_array($priceCountryArray)) { |
122
|
|
|
$priceCountryArray = array(); |
123
|
|
|
} |
124
|
|
|
if ($countryID && in_array($countryID, $priceCountryArray)) { |
125
|
|
|
$priceCanBeUsed = true; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Does it pass the date filter? |
132
|
|
View Code Duplication |
if ($priceCanBeUsed) { |
|
|
|
|
133
|
|
|
$nowTS = strtotime("now"); |
134
|
|
|
if ($price->From) { |
135
|
|
|
$priceCanBeUsed = false; |
136
|
|
|
$fromTS = strtotime($price->From); |
137
|
|
|
if ($fromTS && $fromTS < $nowTS) { |
138
|
|
|
$priceCanBeUsed = true; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
View Code Duplication |
if ($priceCanBeUsed) { |
|
|
|
|
144
|
|
|
if ($price->Until) { |
145
|
|
|
$priceCanBeUsed = false; |
146
|
|
|
$untilTS = strtotime($price->Until); |
147
|
|
|
if ($untilTS && $untilTS > $nowTS) { |
|
|
|
|
148
|
|
|
$priceCanBeUsed = true; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// If so, apply the price |
154
|
|
|
if ($priceCanBeUsed) { |
155
|
|
|
$newPrice = $price->getCalculatedPrice(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ($newPrice > -1) { |
162
|
|
|
$startingPrice = $newPrice; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $startingPrice; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
class ComplexPriceBuyableDecorator_ComplexPriceObject extends DataExtension |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
public static function get_extra_config($class, $extension, $args) |
173
|
|
|
{ |
174
|
|
|
$buyables = EcommerceConfig::get("EcommerceDBConfig", "array_of_buyables"); |
175
|
|
|
$hasOneArray = array(); |
176
|
|
|
if ($buyables && is_array($buyables) && count($buyables)) { |
177
|
|
|
foreach ($buyables as $item) { |
178
|
|
|
$hasOneArray[$item] = $item; |
179
|
|
|
} |
180
|
|
|
return array( |
181
|
|
|
'has_one' => $hasOneArray |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
return array(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function updateCMSFields(FieldList $fields) |
188
|
|
|
{ |
189
|
|
|
$this->owner->getBuyable(); |
190
|
|
|
$buyables = EcommerceConfig::get("EcommerceDBConfig", "array_of_buyables"); |
191
|
|
|
if ($buyables && is_array($buyables) && count($buyables)) { |
192
|
|
|
foreach ($buyables as $item) { |
193
|
|
|
$fields->replaceField($item."ID", new HiddenField($item."ID")); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
$fields->replaceField("From", new TextField("From")); |
197
|
|
|
return $fields; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
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.