1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\FoxyStripe\Extension; |
4
|
|
|
|
5
|
|
|
use Dynamic\FoxyStripe\Model\ProductCartReservation; |
6
|
|
|
use Dynamic\FoxyStripe\Page\ProductPage; |
7
|
|
|
use SilverStripe\Core\Extension; |
8
|
|
|
use SilverStripe\Dev\Debug; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\Forms\HeaderField; |
11
|
|
|
use SilverStripe\Forms\HiddenField; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ProductFormExtension |
15
|
|
|
* @package Dynamic\Sheeps\ProductCartExpiry\Extension |
16
|
|
|
*/ |
17
|
|
|
class PurchaseFormExtension extends Extension |
18
|
|
|
{ |
19
|
|
|
public function updatePurchaseFormFields(FieldList $fields) |
20
|
|
|
{ |
21
|
|
|
if ($this->owner->getProduct()->CartExpiration) { |
22
|
|
|
$fields->insertBefore( |
23
|
|
|
'quantity', |
24
|
|
|
HiddenField::create('expires') |
25
|
|
|
->setValue( |
26
|
|
|
ProductPage::getGeneratedValue( |
27
|
|
|
$this->owner->getProduct()->Code, |
28
|
|
|
'expires', |
29
|
|
|
$this->owner->getProduct()->ExpirationMinutes, |
30
|
|
|
'value' |
31
|
|
|
) |
32
|
|
|
) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param FieldList $fields |
39
|
|
|
*/ |
40
|
|
|
public function updateFoxyStripePurchaseFormActions(FieldList $fields) |
41
|
|
|
{ |
42
|
|
|
if ($this->owner->getProduct()->ControlInventory) { |
43
|
|
|
$reserved = ProductCartReservation::get() |
44
|
|
|
->filter([ |
45
|
|
|
'Code' => $this->owner->getProduct()->Code, |
46
|
|
|
'Expires:GreaterThan' => date('Y-m-d H:i:s', strtotime('now')), |
47
|
|
|
])->count(); |
48
|
|
|
$sold = $this->owner->getProduct()->getNumberPurchased(); |
49
|
|
|
|
50
|
|
|
if ($reserved + $sold >= 1/*$this->owner->getProduct()->PurchaseLimit*/) { |
|
|
|
|
51
|
|
|
$fields->replaceField( |
52
|
|
|
'action_', |
53
|
|
|
HeaderField::create('OutOfStock', 'Out of stock') |
54
|
|
|
->setHeadingLevel(3) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
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.