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