1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\Foxy\Discounts\Extension; |
4
|
|
|
|
5
|
|
|
use Dynamic\Foxy\Discounts\DiscountHelper; |
6
|
|
|
use Dynamic\Foxy\Discounts\Model\Discount; |
7
|
|
|
use Dynamic\Foxy\Discounts\Model\DiscountTier; |
8
|
|
|
use SilverStripe\Forms\DropdownField; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\ORM\DataExtension; |
11
|
|
|
use SilverStripe\ORM\FieldType\DBCurrency; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ProductDataExtension |
15
|
|
|
* @package Dynamic\Foxy\Discounts\Extension |
16
|
|
|
* |
17
|
|
|
* @property bool $ExlcludedFromDiscounts |
18
|
|
|
*/ |
19
|
|
|
class ProductDataExtension extends DataExtension |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string[] |
23
|
|
|
*/ |
24
|
|
|
private static $db = [ |
25
|
|
|
'ExcludeFromDiscounts' => 'Boolean', |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private static $belongs_many_many = [ |
|
|
|
|
32
|
|
|
'Discounts' => Discount::class, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param FieldList $fields |
37
|
|
|
*/ |
38
|
|
|
public function updateCMSFields(FieldList $fields) |
39
|
|
|
{ |
40
|
|
|
$desc = 'Allows, or disallows if this product can be discounted globally. This overrides all other settings.'; |
41
|
|
|
|
42
|
|
|
$fields->addFieldToTab( |
43
|
|
|
'Root.Ecommerce', |
44
|
|
|
DropdownField::create('ExcludeFromDiscounts') |
45
|
|
|
->setSource([ |
46
|
|
|
false => 'Can be discounted', |
47
|
|
|
true => 'Can\'t be discounted', |
48
|
|
|
]) |
49
|
|
|
->setTitle('Can this product be discounted?') |
50
|
|
|
->setDescription($desc), |
51
|
|
|
'Price' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param int $quantity |
57
|
|
|
* @return DiscountHelper |
58
|
|
|
*/ |
59
|
|
|
public function getBestDiscount($quantity = 1) |
60
|
|
|
{ |
61
|
|
|
return DiscountHelper::create($this->owner, $quantity); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param int $quantity |
66
|
|
|
* @return false|DBCurrency |
67
|
|
|
*/ |
68
|
|
|
public function getDiscountPrice($quantity = 1) |
69
|
|
|
{ |
70
|
|
|
if ($discount = $this->getBestDiscount($quantity)) { |
71
|
|
|
return $discount->getDiscountedPrice(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function getHasDiscount() |
81
|
|
|
{ |
82
|
|
|
if ($discount = $this->getBestDiscount()) { |
83
|
|
|
return $discount->getDiscountTier() instanceof DiscountTier; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|