|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class FoxyStripeInventoryManager extends DataExtension |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* @var array |
|
7
|
|
|
*/ |
|
8
|
|
|
private static $db = [ |
|
9
|
|
|
'ControlInventory' => 'Boolean', |
|
10
|
|
|
'PurchaseLimit' => 'Int', |
|
11
|
|
|
'EmbargoLimit' => 'Int', |
|
12
|
|
|
]; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param FieldList $fields |
|
16
|
|
|
*/ |
|
17
|
1 |
|
public function updateCMSFields(FieldList $fields) |
|
18
|
|
|
{ |
|
19
|
1 |
|
$fields->addFieldsToTab('Root.Inventory', array( |
|
20
|
1 |
|
CheckboxField::create('ControlInventory', 'Control Inventory?') |
|
21
|
1 |
|
->setDescription('limit the number of this product available for purchase'), |
|
22
|
1 |
|
NumericField::create('PurchaseLimit') |
|
23
|
1 |
|
->setTitle('Number Available') |
|
24
|
1 |
|
->setDescription('add to cart form will be disabled once number available equals purchased'), |
|
25
|
1 |
|
ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased()), |
|
26
|
1 |
|
NumericField::create('EmbargoLimit') |
|
27
|
1 |
|
->setTitle('Embargo Time') |
|
28
|
1 |
|
->setDescription('time in seconds to reserve an item once added to cart'), |
|
29
|
1 |
|
)); |
|
30
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return int |
|
34
|
|
|
*/ |
|
35
|
2 |
|
public function getNumberPurchased() |
|
36
|
|
|
{ |
|
37
|
2 |
|
$ct = 0; |
|
38
|
2 |
|
if ($this->getOrders()) { |
|
39
|
2 |
|
foreach ($this->getOrders() as $order) { |
|
40
|
2 |
|
$ct += $order->Quantity; |
|
41
|
2 |
|
} |
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
return $ct; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return DataList |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function getOrders() |
|
51
|
|
|
{ |
|
52
|
2 |
|
if ($this->owner->ID) { |
|
53
|
2 |
|
return OrderDetail::get()->filter('ProductID', $this->owner->ID); |
|
54
|
|
|
} |
|
55
|
|
|
return false; |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getIsProductAvailable() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0 && $this->owner->PurchaseLimit < $this->getNumberPurchased(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
class FoxyStripeInventoryManagerExtension extends Extension |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
/** |
|
70
|
|
|
* @param $form |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function updateFoxyStripePurchaseForm(&$form) |
|
73
|
|
|
{ |
|
74
|
1 |
|
if (!$this->owner->getIsProductAvailable()) { |
|
75
|
1 |
|
$form = false; |
|
76
|
1 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.