1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Dynamic\FoxyStripe\Model\FoxyStripeClient; |
4
|
|
|
|
5
|
|
|
class ProductCategory extends DataObject |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
private static $db = array( |
11
|
|
|
'Title' => 'Varchar(255)', |
12
|
|
|
'Code' => 'Varchar(50)', |
13
|
|
|
'DeliveryType' => 'Varchar(50)', |
14
|
|
|
'MaxDownloads' => 'Int', |
15
|
|
|
'MaxDownloadsTime' => 'Int', |
16
|
|
|
'DefaultWeight' => 'Float', |
17
|
|
|
'DefaultWeightUnit' => 'Enum("LBS, KBS", "LBS")', |
18
|
|
|
'DefaultLengthUnit' => 'Enum("in, cm", "in")', |
19
|
|
|
'ShippingFlatRate' => 'Currency', |
20
|
|
|
'ShippingFlatRateType' => 'Varchar(50)', |
21
|
|
|
'HandlingFeeType' => 'Varchar(50)', |
22
|
|
|
'HandlingFee' => 'Currency', |
23
|
|
|
'HandlingFeePercentage' => 'Decimal', |
24
|
|
|
'HandlingFeeMinimum' => 'Currency', |
25
|
|
|
'DiscountType' => 'Varchar(50)', |
26
|
|
|
'DiscountName' => 'Varchar(50)', |
27
|
|
|
'DiscountDetails' => 'Varchar(200)', |
28
|
|
|
'CustomsValue' => 'Currency', |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private static $singular_name = 'FoxyCart Category'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private static $plural_name = 'FoxyCart Categories'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private static $description = 'Set the FoxyCart Category on a Product'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private static $summary_fields = array( |
50
|
|
|
'Title' => 'Name', |
51
|
|
|
'Code' => 'Code' |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private static $indexes = array( |
58
|
|
|
'Code' => true |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return FieldList |
63
|
|
|
*/ |
64
|
|
|
public function getCMSFields() |
65
|
|
|
{ |
66
|
|
|
$fields = parent::getCMSFields(); |
67
|
|
|
|
68
|
|
|
if ($this->ID) { |
69
|
|
|
if ($this->Title == 'Default') { |
|
|
|
|
70
|
|
|
$fields->replaceField( |
71
|
|
|
'Title', |
72
|
|
|
ReadonlyField::create('Title') |
73
|
|
|
); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
$fields->replaceField( |
77
|
1 |
|
'Code', |
78
|
|
|
ReadonlyField::create('Code') |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$fields->insertBefore(HeaderField::create('DeliveryHD', 'Delivery Options', 3), 'DeliveryType'); |
|
|
|
|
83
|
|
|
|
84
|
|
|
$fields->replaceField( |
85
|
1 |
|
'DeliveryType', |
86
|
|
|
OptionsetField::create('DeliveryType', 'Delivery Type', $this->getShippingOptions()) |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$fields->dataFieldByName('MaxDownloads') |
90
|
|
|
->displayIf('DeliveryType')->isEqualTo('downloaded'); |
91
|
|
|
|
92
|
|
|
$fields->dataFieldByName('MaxDownloadsTime') |
93
|
|
|
->displayIf('DeliveryType')->isEqualTo('downloaded'); |
94
|
|
|
|
95
|
|
|
$fields->dataFieldByName('DefaultWeight') |
96
|
|
|
->displayIf('DeliveryType')->isEqualTo('shipped'); |
97
|
|
|
|
98
|
|
|
$fields->dataFieldByName('DefaultWeightUnit') |
99
|
|
|
->displayIf('DeliveryType')->isEqualTo('shipped'); |
100
|
|
|
|
101
|
|
|
$fields->dataFieldByName('DefaultLengthUnit') |
102
|
|
|
->displayIf('DeliveryType')->isEqualTo('shipped'); |
103
|
|
|
|
104
|
|
|
$fields->dataFieldByName('ShippingFlatRate') |
105
|
|
|
->displayIf('DeliveryType')->isEqualTo('flat_rate'); |
106
|
|
|
|
107
|
|
|
$fields->replaceField( |
108
|
|
|
'ShippingFlatRateType', |
109
|
|
|
DropdownField::create('ShippingFlatRateType', 'Flat Rate Type', $this->getShippingFlatRateTypes()) |
110
|
|
|
->setEmptyString('') |
111
|
|
|
->displayIf('DeliveryType')->isEqualTo('flat_rate')->end() |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$fields->insertBefore(HeaderField::create('HandlingHD', 'Handling Fees and Discounts', 3), 'HandlingFeeType'); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$fields->replaceField( |
117
|
|
|
'HandlingFeeType', |
118
|
|
|
DropdownField::create('HandlingFeeType', 'Handling Fee Type', $this->getHandlingFeeTypes()) |
119
|
|
|
->setEmptyString('') |
120
|
|
|
->setDescription('This determines what type of Handling Fee you would like to use.') |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$fields->dataFieldByName('HandlingFee') |
124
|
|
|
->displayIf('HandlingFeeType')->isNotEqualTo(''); |
125
|
|
|
|
126
|
|
|
$fields->dataFieldByName('HandlingFeeMinimum') |
127
|
|
|
->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum'); |
128
|
|
|
|
129
|
|
|
$fields->dataFieldByName('HandlingFeePercentage') |
130
|
|
|
->displayIf('HandlingFeeType')->isEqualTo('flat_percent_with_minimum') |
131
|
|
|
->orIf('HandlingFeeType')->isEqualTo('flat_percent'); |
132
|
|
|
|
133
|
|
|
$fields->replaceField( |
134
|
|
|
'DiscountType', |
135
|
|
|
DropdownField::create('DiscountType', 'Discount Type', $this->getDiscountTypes()) |
136
|
|
|
->setEmptyString('') |
137
|
|
|
->setDescription('This determines what type of per category discount you would like to use, if any.') |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$fields->dataFieldByName('DiscountName') |
141
|
|
|
->displayIf('DiscountType')->isNotEqualTo(''); |
142
|
|
|
|
143
|
|
|
$fields->dataFieldByName('DiscountDetails') |
144
|
|
|
->displayIf('DiscountType')->isNotEqualTo(''); |
145
|
|
|
|
146
|
|
|
$fields->dataFieldByName('CustomsValue') |
147
|
|
|
->setDescription('Enter a dollar amount here for the declared customs value for international shipments. If you leave this blank, the sale price of the item will be used.'); |
148
|
|
|
|
149
|
|
|
/* |
|
|
|
|
150
|
|
|
$fields = FieldList::create( |
151
|
|
|
LiteralField::create( |
152
|
|
|
'PCIntro', |
153
|
|
|
_t( |
154
|
|
|
'ProductCategory.PCIntro', |
155
|
|
|
'<p>Categories must be created in your |
156
|
|
|
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" target="_blank"> |
157
|
|
|
FoxyCart Product Categories |
158
|
|
|
</a>, and also manually created in FoxyStripe. |
159
|
|
|
</p>' |
160
|
|
|
) |
161
|
|
|
), |
162
|
|
|
TextField::create('Code') |
163
|
|
|
->setTitle(_t('ProductCategory.Code', 'Category Code')) |
164
|
|
|
->setDescription(_t('ProductCategory.CodeDescription', 'copy/paste from FoxyCart')), |
165
|
|
|
TextField::create('Title') |
166
|
|
|
->setTitle(_t('ProductCategory.Title', 'Category Title')) |
167
|
|
|
->setDescription(_t('ProductCategory.TitleDescription', 'copy/paste from FoxyCart')), |
168
|
|
|
DropdownField::create( |
169
|
|
|
'DeliveryType', |
170
|
|
|
'Delivery Type', |
171
|
|
|
singleton('ProductCategory')->dbObject('DeliveryType')->enumValues() |
172
|
|
|
)->setEmptyString('') |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$this->extend('updateCMSFields', $fields); |
176
|
|
|
*/ |
177
|
|
|
|
178
|
|
|
return $fields; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* |
183
|
|
|
*/ |
184
|
|
|
public function requireDefaultRecords() |
185
|
|
|
{ |
186
|
|
|
parent::requireDefaultRecords(); |
187
|
|
|
$allCats = DataObject::get('ProductCategory'); |
188
|
|
|
if (!$allCats->count()) { |
189
|
|
|
$cat = new ProductCategory(); |
190
|
|
|
$cat->Title = 'Default'; |
|
|
|
|
191
|
|
|
$cat->Code = 'DEFAULT'; |
|
|
|
|
192
|
|
|
$cat->write(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param bool $member |
198
|
|
|
* @return bool |
199
|
|
|
*/ |
200
|
|
|
public function canView($member = false) |
201
|
|
|
{ |
202
|
|
|
return true; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param null $member |
207
|
|
|
* @return bool|int |
208
|
|
|
*/ |
209
|
|
|
public function canEdit($member = null) |
210
|
|
|
{ |
211
|
|
|
return Permission::check('Product_CANCRUD', 'any', $member); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param null $member |
216
|
|
|
* @return bool|int |
217
|
|
|
*/ |
218
|
|
|
public function canDelete($member = null) |
219
|
|
|
{ |
220
|
|
|
|
221
|
|
|
//don't allow deletion of DEFAULT category |
222
|
|
|
return ($this->Code == 'DEFAULT') ? false : Permission::check('Product_CANCRUD', 'any', $member); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param null $member |
227
|
|
|
* @return bool|int |
228
|
|
|
*/ |
229
|
|
|
public function canCreate($member = null) |
230
|
|
|
{ |
231
|
|
|
return Permission::check('Product_CANCRUD', 'any', $member); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return array |
236
|
|
|
*/ |
237
|
|
|
public function getShippingOptions() |
238
|
|
|
{ |
239
|
|
|
return [ |
240
|
|
|
'shipped' => 'Shipped using live shipping rates', |
241
|
|
|
'downloaded' => 'Downloaded by the customer', |
242
|
|
|
'flat_rate' => 'Shipped using a flat rate fee', |
243
|
|
|
'pickup' => 'Picked up by the customer', |
244
|
|
|
'notshipped' => 'No Shipping', |
245
|
|
|
]; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return array |
250
|
|
|
*/ |
251
|
|
|
public function getShippingFlatRateTypes() |
252
|
|
|
{ |
253
|
|
|
return [ |
254
|
|
|
'per_order' => 'Charge per order', |
255
|
|
|
'per_item' => 'Charge per item', |
256
|
|
|
]; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return array |
261
|
|
|
*/ |
262
|
|
|
public function getHandlingFeeTypes() |
263
|
|
|
{ |
264
|
|
|
return [ |
265
|
|
|
'flat_per_order' => 'Flat fee per order with products in this category', |
266
|
|
|
'flat_per_item' => 'Flat fee per product in this category', |
267
|
|
|
'flat_percent' => 'Flat fee per shipment + % of price for products in this category', |
268
|
|
|
'flat_percent_with_minimum' => 'Flat fee per shipment OR % of order total with products in this category. Whichever is greater.', |
269
|
|
|
]; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
public function getDiscountTypes() |
276
|
|
|
{ |
277
|
|
|
return [ |
278
|
|
|
'quantity_amount' => 'Discount by an amount based on the quantity', |
279
|
|
|
'quantity_percentage' => 'Discount by a percentage based on the quantity', |
280
|
|
|
'price_amount' => 'Discount by an amount based on the price in this category', |
281
|
|
|
'price_percentage' => 'Discount by a percentage based on the price in this category', |
282
|
|
|
]; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
|
public function getDataMap() |
289
|
|
|
{ |
290
|
|
|
return [ |
291
|
|
|
'name' => $this->Title, |
|
|
|
|
292
|
|
|
'code' => $this->Code, |
|
|
|
|
293
|
|
|
'item_delivery_type' => $this->DeliveryType, |
|
|
|
|
294
|
|
|
'max_downloads_per_customer' => $this->MaxDownloads, |
|
|
|
|
295
|
|
|
'max_downloads_time_period' => $this->MaxDownloadsTime, |
|
|
|
|
296
|
|
|
'customs_value' => $this->CustomsValue, |
|
|
|
|
297
|
|
|
'default_weight' => $this->DefaultWeight, |
|
|
|
|
298
|
|
|
'default_weight_unit' => $this->DefaultWeightUnit, |
|
|
|
|
299
|
|
|
'default_length_unit' => $this->DefautlLengthUnit, |
|
|
|
|
300
|
|
|
'shipping_flat_rate' => $this->ShippingFlatRate, |
|
|
|
|
301
|
|
|
'shipping_flat_rate_type' => $this->ShippingFlatRateType, |
|
|
|
|
302
|
|
|
'handling_fee_type' => $this->HandlingFeeType, |
|
|
|
|
303
|
|
|
'handling_fee' => $this->HandlingFee, |
|
|
|
|
304
|
|
|
'handling_fee_minimum' => $this->HandlingFeeMinimum, |
|
|
|
|
305
|
|
|
'handling_fee_percentage' => $this->HandlingFeePercentage, |
|
|
|
|
306
|
|
|
'discount_type' => $this->DiscountType, |
|
|
|
|
307
|
|
|
'discount_name' => $this->DiscountName, |
|
|
|
|
308
|
|
|
'discount_details' => $this->DiscountDetails, |
|
|
|
|
309
|
|
|
]; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* |
314
|
|
|
*/ |
315
|
|
|
public function onAfterWrite() |
316
|
|
|
{ |
317
|
|
|
parent::onAfterWrite(); |
318
|
|
|
|
319
|
|
|
if ($this->isChanged()) { |
320
|
|
|
if ($fc = new FoxyStripeClient()) { |
321
|
|
|
$fc->putCategory($this->getDataMap()); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* |
328
|
|
|
*/ |
329
|
|
|
public function onAfterDelete() |
330
|
|
|
{ |
331
|
|
|
parent::onAfterDelete(); |
332
|
|
|
|
333
|
|
|
if ($fc = new FoxyStripeClient()) { |
334
|
|
|
$fc->deleteCategory($this->getDataMap()); |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.