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->removeByName(array( |
20
|
1 |
|
'PurchaseLimit', |
21
|
1 |
|
'EmbargoLimit', |
22
|
1 |
|
'NumberPurchased', |
23
|
1 |
|
)); |
24
|
|
|
|
25
|
1 |
|
$fields->addFieldsToTab('Root.Inventory', array( |
26
|
1 |
|
CheckboxField::create('ControlInventory', 'Control Inventory?') |
27
|
1 |
|
->setDescription('limit the number of this product available for purchase'), |
28
|
1 |
|
DisplayLogicWrapper::create( |
29
|
1 |
|
NumericField::create('PurchaseLimit') |
30
|
1 |
|
->setTitle('Number Available') |
31
|
1 |
|
->setDescription('add to cart form will be disabled once number available equals purchased'), |
32
|
1 |
|
ReadonlyField::create('NumberPurchased', 'Purchased', $this->getNumberPurchased())//, |
33
|
|
|
/* |
|
|
|
|
34
|
|
|
NumericField::create('EmbargoLimit') |
35
|
|
|
->setTitle('Embargo Time') |
36
|
|
|
->setDescription('time in seconds to reserve an item once added to cart') |
37
|
|
|
*/ |
38
|
1 |
|
)->displayIf('ControlInventory')->isChecked()->end(), |
39
|
1 |
|
)); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
1 |
|
public function getHasInventory() |
46
|
|
|
{ |
47
|
1 |
|
return $this->owner->ControlInventory && $this->owner->PurchaseLimit != 0; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
1 |
|
public function getIsProductAvailable() |
54
|
|
|
{ |
55
|
1 |
|
if ($this->owner->getHasInventory()) { |
56
|
1 |
|
return $this->owner->PurchaseLimit > $this->getNumberPurchased(); |
57
|
|
|
} |
58
|
|
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getNumberAvailable() |
62
|
|
|
{ |
63
|
|
|
if ($this->getIsProductAvailable()) { |
64
|
|
|
return (int)$this->owner->PurchaseLimit - (int)$this->getNumberPurchased(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
2 |
|
public function getNumberPurchased() |
73
|
|
|
{ |
74
|
2 |
|
$ct = 0; |
75
|
2 |
|
if ($this->getOrders()) { |
76
|
2 |
|
foreach ($this->getOrders() as $order) { |
77
|
2 |
|
$ct += $order->Quantity; |
78
|
2 |
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
return $ct; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return DataList |
86
|
|
|
*/ |
87
|
2 |
|
public function getOrders() |
88
|
|
|
{ |
89
|
2 |
|
if ($this->owner->ID) { |
90
|
2 |
|
return OrderDetail::get()->filter('ProductID', $this->owner->ID); |
91
|
|
|
} |
92
|
|
|
return false; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
class FoxyStripeInventoryManagerExtension extends Extension |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
/** |
99
|
|
|
* @param $form |
100
|
|
|
*/ |
101
|
1 |
|
public function updateFoxyStripePurchaseForm(&$form) |
102
|
|
|
{ |
103
|
1 |
|
if (!$this->owner->getIsProductAvailable()) { |
104
|
1 |
|
$form = false; |
105
|
1 |
|
return; |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
if ($this->owner->getHasInventory()) { |
109
|
|
|
$quantityMax = $this->owner->getNumberAvailable(); |
110
|
|
|
$count = 1; |
111
|
|
|
$quantity = array(); |
112
|
|
|
while ($count <= $quantityMax) { |
113
|
|
|
$countVal = ProductPage::getGeneratedValue($this->owner->Code, 'quantity', $count, 'value'); |
114
|
|
|
$quantity[$countVal] = $count; |
115
|
|
|
$count++; |
116
|
|
|
} |
117
|
|
|
$fields = $form->Fields(); |
118
|
|
|
$fields->replaceField('quantity', DropdownField::create('quantity', 'Quantity', $quantity)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.