1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverCommerce\Discounts\Forms; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
6
|
|
|
use SilverStripe\Forms\FieldList; |
7
|
|
|
use SilverStripe\Forms\TextField; |
8
|
|
|
use SilverStripe\Forms\FormAction; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use SilverStripe\Forms\RequiredFields; |
11
|
|
|
use SilverStripe\Control\RequestHandler; |
12
|
|
|
use SilverCommerce\Discounts\DiscountFactory; |
13
|
|
|
use SilverCommerce\OrdersAdmin\Model\Estimate; |
14
|
|
|
use SilverCommerce\ShoppingCart\ShoppingCartFactory; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A simple form to add a discount to an estimate via the code. |
18
|
|
|
*/ |
19
|
|
|
class DiscountCodeForm extends Form |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* ID of the estimate the discount will be applied to. |
23
|
|
|
* |
24
|
|
|
* @var Int |
25
|
|
|
*/ |
26
|
|
|
protected $estimate_id; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
RequestHandler $controller = null, |
30
|
|
|
$name = self::DEFAULT_NAME, |
31
|
|
|
Estimate $estimate |
32
|
|
|
) { |
33
|
|
|
$this->estimate_id = $estimate->ID; |
34
|
|
|
|
35
|
|
|
$fields = FieldList::create( |
36
|
|
|
TextField::create( |
37
|
|
|
"DiscountCode", |
38
|
|
|
_t("ShoppingCart.DiscountCode", "Discount Code") |
39
|
|
|
)->setAttribute( |
40
|
|
|
"placeholder", |
41
|
|
|
_t("ShoppingCart.EnterDiscountCode", "Enter a discount code") |
42
|
|
|
) |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$actions = FieldList::create( |
46
|
|
|
FormAction::create( |
47
|
|
|
'doAddDiscount', |
48
|
|
|
_t('ShoppingCart.Add', 'Add') |
49
|
|
|
)->addExtraClass('btn btn-info') |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$validator = new RequiredFields( |
53
|
|
|
[ |
54
|
|
|
"DiscountCode" |
55
|
|
|
] |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
59
|
|
|
|
60
|
|
|
$this->setTemplate( |
61
|
|
|
__NAMESPACE__ . 'Includes\\'. __CLASS__ |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* retrieve the estimate via the id. |
67
|
|
|
* |
68
|
|
|
* @return Estimate |
69
|
|
|
*/ |
70
|
|
|
public function getEstimate() |
71
|
|
|
{ |
72
|
|
|
return Estimate::get()->byID($this->estimate_id); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Action that will find a discount based on the code |
77
|
|
|
* |
78
|
|
|
* @param type $data |
79
|
|
|
* @param type $form |
|
|
|
|
80
|
|
|
*/ |
81
|
|
|
public function doAddDiscount($data, $form) |
82
|
|
|
{ |
83
|
|
|
$code_to_search = $data['DiscountCode']; |
84
|
|
|
$cart = ShoppingCartFactory::create(); |
85
|
|
|
$existing = $cart->getOrder()->Discounts(); |
86
|
|
|
$limit = Config::inst()->get(ShoppingCartFactory::class, 'discount_limit'); |
87
|
|
|
|
88
|
|
|
if ($limit && $existing->exists() && $existing->count() >= $limit) { |
89
|
|
|
$form->sessionMessage("Only ".$limit." discount code(s) can be used at a time.", 'bad'); |
90
|
|
|
return $this->getRequestHandler()->redirectBack(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$discount = DiscountFactory::create()->getByIdent($code_to_search); |
94
|
|
|
|
95
|
|
|
if (!$discount) { |
96
|
|
|
$form->sessionMessage("The entered code is invalid.", 'bad'); |
97
|
|
|
} else { |
98
|
|
|
$estimate = $this->getEstimate(); |
99
|
|
|
// First check if the discount is already added (so we don't |
100
|
|
|
// query the DB if we don't have to). |
101
|
|
|
if (!$estimate->findDiscount($code_to_search)) { |
102
|
|
|
DiscountFactory::create()->generateAppliedDiscount($code_to_search, $estimate); |
103
|
|
|
$cart->save(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->getRequestHandler()->redirectBack(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths