Browse code

Update

Benjamin Roth authored on20/03/2023 16:19:16
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,231 @@
1
+<?php
2
+
3
+/**
4
+ * Custom extension for Isotope for Contao
5
+ *
6
+ * Copyright (c) 2014 Benjamin Roth [http://www.esales-media.de]
7
+ *
8
+ * @package eSM_isotope_custom
9
+ * @link    http://www.esales-media.de
10
+ * @license commercial
11
+ */
12
+
13
+namespace eSM_isotope_custom;
14
+
15
+use Isotope\Interfaces\IsotopePrice;
16
+use Isotope\Interfaces\IsotopeProductCollection;
17
+use Isotope\Model\ProductCollection\Order;
18
+use eSM_isotope_custom\Model\AffentalerRule as Rule;
19
+use Isotope\Isotope;
20
+use Isotope\Model\ProductCollection\Cart;
21
+//use Isotope\Module\Cart;
22
+use eSM_isotope_custom\Model\ProductCollectionSurcharge\AffentalerRule as RuleSurcharge;
23
+
24
+
25
+class IsotopeHooks extends \Controller
26
+{
27
+    public function addProductToCollection($objProduct, $intQuantity, $ProductCollection)
28
+    {
29
+        if ($objProduct->not_buyable)
30
+        {
31
+          return 0;
32
+        }
33
+        return $intQuantity;
34
+    }
35
+
36
+  /**
37
+   * Calculate the price for a product, applying rules and coupons
38
+   *
39
+   * @param    float
40
+   * @param    object
41
+   * @param    string
42
+   * @param    int
43
+   * @return float
44
+   */
45
+  public function calculatePrice($fltPrice, $objSource, $strField, $intTaxClass)
46
+  {
47
+    // RRP
48
+    if ($strField === 'original_price' && ($objProduct = $objSource->getRelated('pid')) !== null)
49
+    {
50
+      if ($objProduct->price_rrp > $fltPrice)
51
+      {
52
+        return $objProduct->price_rrp;
53
+      }
54
+    }
55
+
56
+    if ($objSource instanceof IsotopePrice && ('price' === $strField || 'low_price' === $strField || 'net_price' === $strField || 'gross_price' === $strField)) {
57
+
58
+      // @todo try not to use getRelated() because it loads variants
59
+      $objRules = Rule::findByProduct($objSource->getRelated('pid'), $strField, $fltPrice);
60
+
61
+      if (null !== $objRules) {
62
+        while ($objRules->next()) {
63
+          // Check cart quantity
64
+          if ($objRules->minItemQuantity > 0 || $objRules->maxItemQuantity >= 0) {
65
+            if ('cart_products' === $objRules->quantityMode) {
66
+              $intTotal = Isotope::getCart()->countItems();
67
+            } elseif ('cart_items' === $objRules->quantityMode) {
68
+              $intTotal = Isotope::getCart()->sumItemsQuantity();
69
+            } else {
70
+              $objItem = Isotope::getCart()->getItemForProduct($objSource->getRelated('pid'));
71
+              $intTotal = (null === $objItem) ? 0 : $objItem->quantity;
72
+            }
73
+
74
+            if (($objRules->minItemQuantity > 0 && $objRules->minItemQuantity > $intTotal) || $objRules->maxItemQuantity < $intTotal) {
75
+              continue;
76
+            }
77
+          }
78
+
79
+          // We're unable to apply variant price rules to low_price (see #3189)
80
+          if ('low_price' === $strField && 'variants' === $objRules->productRestrictions) {
81
+            continue;
82
+          }
83
+
84
+          if ($objRules->current()->isPercentage()) {
85
+            $fltDiscount = 100 + $objRules->current()->getPercentage();
86
+            $fltDiscount = round($fltPrice - ($fltPrice / 100 * $fltDiscount), 10);
87
+
88
+            $precision = Isotope::getConfig()->priceRoundPrecision;
89
+            $factor    = pow(10, 2);
90
+            $up        = $fltDiscount > 0 ? 'ceil' : 'floor';
91
+            $down      = $fltDiscount > 0 ? 'floor' : 'ceil';
92
+
93
+            switch ($objRules->rounding) {
94
+              case Rule::ROUND_NORMAL:
95
+                $fltDiscount = round($fltDiscount, $precision);
96
+                break;
97
+
98
+              case Rule::ROUND_UP:
99
+                $fltDiscount = $up($fltDiscount * $factor) / $factor;
100
+                break;
101
+
102
+              case Rule::ROUND_DOWN:
103
+              default:
104
+                $fltDiscount = $down($fltDiscount * $factor) / $factor;
105
+                break;
106
+            }
107
+
108
+            $fltPrice = $fltPrice - $fltDiscount;
109
+          } else {
110
+            $fltPrice = $fltPrice + $objRules->discount;
111
+          }
112
+        }
113
+      }
114
+    }
115
+
116
+    return $fltPrice;
117
+  }
118
+
119
+  /**
120
+   * Pattern for bulk image file import
121
+   *
122
+   * @param $arrPattern
123
+   * @param $objProducts
124
+   * @return array
125
+   */
126
+  public function addAssetImportRegexp($arrPattern, $objProducts)
127
+  {
128
+    $arrPattern[] = $objProducts->sku ? '[0]*'.$objProducts->sku : null;
129
+
130
+    return $arrPattern;
131
+  }
132
+
133
+  public function modifyAddressFields($arrFields, $objAddress, $objStep)
134
+  {
135
+    foreach ($arrFields as $fieldName=>$field)
136
+    {
137
+      if ($fieldName == 'dateOfBirth')
138
+      {
139
+        $arrFields[$fieldName]['dca']['eval']['rgxp'] = 'date_age16';
140
+      }
141
+    }
142
+    return $arrFields;
143
+  }
144
+
145
+  public function addCustomRegexp($strRegexp, &$varInput, \Widget $objWidget)
146
+  {
147
+    switch ($strRegexp)
148
+    {
149
+      case 'date_age16':
150
+        if (!\Validator::isDate($varInput))
151
+        {
152
+          $objWidget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['date'], \Date::getInputFormat(\Date::getNumericDateFormat())));
153
+          return true;
154
+        }
155
+        else
156
+        {
157
+          // Validate the date (see #5086)
158
+          try
159
+          {
160
+            new \Date($varInput, \Date::getNumericDateFormat());
161
+          }
162
+          catch (\OutOfBoundsException $e)
163
+          {
164
+            $objWidget->addError(sprintf($GLOBALS['TL_LANG']['ERR']['invalidDate'], $varInput));
165
+          }
166
+
167
+          // Age >= 16
168
+          $Date = new \Date($varInput, \Date::getNumericDateFormat());
169
+          $MinDate = new \Date(mktime(0,0,0,idate('m'),idate('d'),(idate('Y')-16)));
170
+
171
+          if ($Date->tstamp > $MinDate->tstamp)
172
+          {
173
+            $objWidget->addError($GLOBALS['TL_LANG']['ERR']['date_age16']);
174
+            return true;
175
+          }
176
+
177
+        }
178
+        break;
179
+    }
180
+
181
+    return false;
182
+  }
183
+
184
+  public function getAttributesFromDca($arrAttributes, $objDca)
185
+  {
186
+    // modify $arrAttributes as needed
187
+    // Convert timestamps
188
+    if ($arrAttributes['value'] != '' && in_array($arrAttributes['rgxp'], array('date_age16')))
189
+    {
190
+      $objDate = new \Date($arrAttributes['value'], \Date::getFormatFromRgxp('date'));
191
+      $arrAttributes['value'] = $objDate->date;
192
+    }
193
+
194
+    return $arrAttributes;
195
+  }
196
+
197
+  /**
198
+   * Add cart rules to surcharges
199
+   */
200
+  public function findSurcharges(IsotopeProductCollection $objCollection)
201
+  {
202
+    $objCart = $objCollection;
203
+
204
+    // The checkout review pages shows an order, but we need the cart
205
+    // Only the cart contains coupons etc.
206
+    if ($objCollection instanceof Order) {
207
+      $objCart = $objCollection->getRelated('source_collection_id');
208
+    }
209
+
210
+    // Rules should only be applied to Cart, not any other product collection
211
+    if (!($objCart instanceof Cart)) {
212
+      return array();
213
+    }
214
+
215
+    $arrSurcharges = array();
216
+    $objRules = Rule::findForCart();
217
+
218
+    if (null !== $objRules) {
219
+      while ($objRules->next()) {
220
+        $objSurcharge = RuleSurcharge::createForRuleInCollection($objRules->current(), $objCollection);
221
+
222
+        if (null !== $objSurcharge) {
223
+          $arrSurcharges[] = $objSurcharge;
224
+        }
225
+      }
226
+    }
227
+
228
+    return $arrSurcharges;
229
+  }
230
+
231
+}
0 232
\ No newline at end of file