|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Nicolaas [at] sunnysideup.co.nz |
|
5
|
|
|
* @package: ecommerce |
|
6
|
|
|
* @sub-package: ecommerce_delivery |
|
7
|
|
|
* @description: This modifier check for Combination Products |
|
8
|
|
|
* If it find a combination product it: |
|
9
|
|
|
* |
|
10
|
|
|
* a. adds all the relevent combination products |
|
11
|
|
|
* b. checks for quantities |
|
12
|
|
|
* |
|
13
|
|
|
**/ |
|
14
|
|
|
class CombinationProductModifier extends OrderModifier |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
// ######################################## *** other (non) static variables (e.g. private static $special_name_for_something, protected $order) |
|
18
|
|
|
|
|
19
|
|
|
private static $savings = 0; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* standard modifier method |
|
24
|
|
|
* @param Boolean $force - should the update run no matter what |
|
25
|
|
|
*/ |
|
26
|
|
|
public function runUpdate($force = false) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->addProductsPerCombo(); |
|
29
|
|
|
parent::runUpdate($force); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
// ######################################## *** form functions (e. g. showform and getform) |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* standard OrderModifier Method |
|
37
|
|
|
* Should we show a form in the checkout page for this modifier? |
|
38
|
|
|
*/ |
|
39
|
|
|
public function ShowForm() |
|
40
|
|
|
{ |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// ######################################## *** template functions (e.g. ShowInTable, TableTitle, etc...) ... USES DB VALUES |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* standard OrderModifer Method |
|
48
|
|
|
* Tells us if the modifier should take up a row in the table on the checkout page. |
|
49
|
|
|
* @return Boolean |
|
50
|
|
|
*/ |
|
51
|
|
|
public function ShowInTable() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->loadCombinationProducts()->count() ? true: false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* standard OrderModifer Method |
|
58
|
|
|
* Tells us if the modifier can be removed (hidden / turned off) from the order. |
|
59
|
|
|
* @return Boolean |
|
60
|
|
|
*/ |
|
61
|
|
|
public function CanBeRemoved() |
|
62
|
|
|
{ |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// ######################################## *** inner calculations.... USES CALCULATED VALUES |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* loads the items in the static variable $order_items |
|
70
|
|
|
* and saves the items for future use. |
|
71
|
|
|
* @return Null | DataObjectSet |
|
|
|
|
|
|
72
|
|
|
*/ |
|
73
|
|
|
protected function loadIncludedProductItems() |
|
74
|
|
|
{ |
|
75
|
|
|
return IncludedProduct_OrderItem::get()->filter(array("OrderID" => $this->Order()->ID)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* loads the items in the static variable $order_items |
|
80
|
|
|
* and saves the items for future use. |
|
81
|
|
|
* @return Null | DataObjectSet |
|
|
|
|
|
|
82
|
|
|
*/ |
|
83
|
|
|
protected function loadCombinationProducts() |
|
84
|
|
|
{ |
|
85
|
|
|
return CombinationProduct_OrderItem::get()->filter(array("OrderID" => $this->Order()->ID)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* checks for Combination Products and makes sure that enough of the component products are added. |
|
90
|
|
|
* |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function addProductsPerCombo() |
|
93
|
|
|
{ |
|
94
|
|
|
$reload = false; |
|
95
|
|
|
$shoppingCart = ShoppingCart::singleton(); |
|
96
|
|
|
$combinationProductOrderItems = $this->loadCombinationProducts(); |
|
97
|
|
|
if ($combinationProductOrderItems->count()) { |
|
98
|
|
|
foreach ($combinationProductOrderItems as $combinationProductOrderItem) { |
|
99
|
|
|
$combinationProduct = $combinationProductOrderItem->Buyable(); |
|
100
|
|
|
$comboProductQTY = $combinationProductOrderItem->Quantity; |
|
101
|
|
|
$includedProducts = $combinationProduct->IncludedProducts(); |
|
102
|
|
|
if ($includedProducts) { |
|
103
|
|
|
foreach ($includedProducts as $includedProduct) { |
|
104
|
|
|
$includedProductQTY = $this->countOfProductInOrder($includedProduct); |
|
105
|
|
|
$difference = $comboProductQTY - $includedProductQTY; |
|
106
|
|
|
if ($difference) { |
|
107
|
|
|
$reload = true; |
|
108
|
|
|
if ($comboProductQTY) { |
|
109
|
|
|
//in case it has not been added |
|
110
|
|
|
if (!$includedProduct->IsInCart()) { |
|
111
|
|
|
$includedProduct->setAlternativeClassNameForOrderItem("IncludedProduct_OrderItem"); |
|
112
|
|
|
$item = $shoppingCart->addBuyable($includedProduct); |
|
113
|
|
|
if ($item) { |
|
114
|
|
|
$item->ParentOrderItemID = $combinationProductOrderItem->ID; |
|
115
|
|
|
$item->write(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
$shoppingCart->setQuantity($includedProduct, $comboProductQTY); |
|
119
|
|
|
} else { |
|
120
|
|
|
$shoppingCart->deleteBuyable($includedProduct); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
if ($reload) { |
|
128
|
|
|
CartResponse::set_force_reload(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Tells us the number of times a product has been added to the order (quantity) |
|
134
|
|
|
* @param Product $product - the product we are checking. |
|
135
|
|
|
* @return Integer |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function countOfProductInOrder($product) |
|
138
|
|
|
{ |
|
139
|
|
|
if ($items = $this->loadIncludedProductItems()) { |
|
140
|
|
|
foreach ($items as $item) { |
|
141
|
|
|
$buyable = $item->Buyable(); |
|
142
|
|
|
if ($buyable) { |
|
143
|
|
|
if ($product->ClassName == $buyable->ClassName && $product->ID == $buyable->ID) { |
|
144
|
|
|
return $item->Quantity; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// ######################################## *** calculate database fields: protected function Live[field name] ... USES CALCULATED VALUES |
|
152
|
|
|
|
|
153
|
|
|
public function LiveName() |
|
154
|
|
|
{ |
|
155
|
|
|
return _t("CombinationProductModifier.SAVINGS", "Savings"); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function LiveTableValue() |
|
159
|
|
|
{ |
|
160
|
|
|
self::$savings = 0; |
|
161
|
|
|
if ($this->ShowInTable()) { |
|
162
|
|
|
$combinationProductOrderItems = CombinationProduct_OrderItem::get()->filter(array("OrderID" => $this->Order()->ID)); |
|
163
|
|
|
if ($combinationProductOrderItems->count()) { |
|
164
|
|
|
foreach ($combinationProductOrderItems as $combinationProductOrderItem) { |
|
165
|
|
|
$buyable = $combinationProductOrderItem->Buyable(); |
|
166
|
|
|
self::$savings -= ($buyable->getPrice() - $buyable->NewPrice) * $combinationProductOrderItem->Quantity; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
return self::$savings; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
// ######################################## *** Type Functions (IsChargeable, IsDeductable, IsNoChange, IsRemoved) |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
// ######################################## *** standard database related functions (e.g. onBeforeWrite, onAfterWrite, etc...) |
|
177
|
|
|
|
|
178
|
|
|
// ######################################## *** AJAX related functions |
|
179
|
|
|
|
|
180
|
|
|
// ######################################## *** debug functions |
|
181
|
|
|
} |
|
182
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.