9
9
use Magento \Catalog \Model \ProductFactory ;
10
10
use Magento \CatalogRule \Model \ResourceModel \Rule ;
11
11
use Magento \Customer \Model \Group ;
12
+ use Magento \Customer \Api \Data \GroupInterface ;
12
13
use Magento \Customer \Model \ResourceModel \Group \CollectionFactory ;
13
14
use Magento \Framework \Pricing \PriceCurrencyInterface ;
14
15
use Magento \Tax \Helper \Data as TaxHelper ;
@@ -110,6 +111,7 @@ public function addPriceData($customData, Product $product, $subProducts): array
110
111
$ this ->customData [$ field ][$ currencyCode ]['default ' ] = $ this ->priceCurrency ->round ($ price );
111
112
$ this ->customData [$ field ][$ currencyCode ]['default_formated ' ] = $ this ->formatPrice ($ price , $ currencyCode );
112
113
$ specialPrice = $ this ->getSpecialPrice ($ product , $ currencyCode , $ withTax );
114
+ $ tierPrice = $ this ->getTierPrice ($ product , $ currencyCode , $ withTax );
113
115
if ($ this ->areCustomersGroupsEnabled ) {
114
116
$ this ->addCustomerGroupsPrices ($ product , $ currencyCode , $ withTax , $ field );
115
117
}
@@ -118,6 +120,7 @@ public function addPriceData($customData, Product $product, $subProducts): array
118
120
$ this ->customData [$ field ][$ currencyCode ]['special_to_date ' ] =
119
121
(!empty ($ product ->getSpecialToDate ())) ? strtotime ($ product ->getSpecialToDate ()) : '' ;
120
122
$ this ->addSpecialPrices ($ specialPrice , $ field , $ currencyCode );
123
+ $ this ->addTierPrices ($ tierPrice , $ field , $ currencyCode );
121
124
$ this ->addAdditionalData ($ product , $ withTax , $ subProducts , $ currencyCode , $ field );
122
125
}
123
126
}
@@ -228,6 +231,96 @@ protected function getSpecialPrice(Product $product, $currencyCode, $withTax): a
228
231
}
229
232
return $ specialPrice ;
230
233
}
234
+
235
+ /**
236
+ * @param Product $product
237
+ * @param $currencyCode
238
+ * @param $withTax
239
+ * @return array
240
+ */
241
+ protected function getTierPrice (Product $ product , $ currencyCode , $ withTax )
242
+ {
243
+ $ tierPrice = [];
244
+ $ tierPrices = [];
245
+
246
+ if (!is_null ($ product ->getTierPrices ())) {
247
+ $ product ->setData ('website_id ' , $ product ->getStore ()->getWebsiteId ());
248
+ $ productTierPrices = $ product ->getTierPrices ();
249
+ foreach ($ productTierPrices as $ productTierPrice ) {
250
+ if (!isset ($ tierPrices [$ productTierPrice ->getCustomerGroupId ()])) {
251
+ $ tierPrices [$ productTierPrice ->getCustomerGroupId ()] = $ productTierPrice ->getValue ();
252
+
253
+ continue ;
254
+ }
255
+
256
+ $ tierPrices [$ productTierPrice ->getCustomerGroupId ()] = min (
257
+ $ tierPrices [$ productTierPrice ->getCustomerGroupId ()],
258
+ $ productTierPrice ->getValue ()
259
+ );
260
+ }
261
+ }
262
+
263
+ /** @var Group $group */
264
+ foreach ($ this ->groups as $ group ) {
265
+ $ groupId = (int ) $ group ->getData ('customer_group_id ' );
266
+ $ tierPrice [$ groupId ] = false ;
267
+
268
+ $ currentTierPrice = null ;
269
+ if (!isset ($ tierPrices [$ groupId ]) && !isset ($ tierPrices [GroupInterface::CUST_GROUP_ALL ])) {
270
+ continue ;
271
+ }
272
+
273
+ if (isset ($ tierPrices [GroupInterface::CUST_GROUP_ALL ])
274
+ && $ tierPrices [GroupInterface::CUST_GROUP_ALL ] !== []) {
275
+ $ currentTierPrice = $ tierPrices [GroupInterface::CUST_GROUP_ALL ];
276
+ }
277
+
278
+ if (isset ($ tierPrices [$ groupId ]) && $ tierPrices [$ groupId ] !== []) {
279
+ $ currentTierPrice = $ currentTierPrice === null ?
280
+ $ tierPrices [$ groupId ] :
281
+ min ($ currentTierPrice , $ tierPrices [$ groupId ]);
282
+ }
283
+
284
+ if ($ currencyCode !== $ this ->baseCurrencyCode ) {
285
+ $ currentTierPrice =
286
+ $ this ->priceCurrency ->round ($ this ->convertPrice ($ currentTierPrice , $ currencyCode ));
287
+ }
288
+ $ tierPrice [$ groupId ] = $ this ->getTaxPrice ($ product , $ currentTierPrice , $ withTax );
289
+ }
290
+
291
+ return $ tierPrice ;
292
+ }
293
+
294
+ /**
295
+ * @param $tierPrice
296
+ * @param $field
297
+ * @param $currencyCode
298
+ * @return void
299
+ */
300
+ protected function addTierPrices ($ tierPrice , $ field , $ currencyCode )
301
+ {
302
+ if ($ this ->areCustomersGroupsEnabled ) {
303
+ /** @var Group $group */
304
+ foreach ($ this ->groups as $ group ) {
305
+ $ groupId = (int ) $ group ->getData ('customer_group_id ' );
306
+
307
+ if ($ tierPrice [$ groupId ]) {
308
+ $ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId . '_tier ' ] = $ tierPrice [$ groupId ];
309
+
310
+ $ this ->customData [$ field ][$ currencyCode ]['group_ ' . $ groupId . '_tier_formated ' ] =
311
+ $ this ->formatPrice ($ tierPrice [$ groupId ], $ currencyCode );
312
+ }
313
+ }
314
+
315
+ return ;
316
+ }
317
+
318
+ if ($ tierPrice [0 ]) {
319
+ $ this ->customData [$ field ][$ currencyCode ]['default_tier ' ] = $ this ->priceCurrency ->round ($ tierPrice [0 ]);
320
+ $ this ->customData [$ field ][$ currencyCode ]['default_tier_formated ' ] =
321
+ $ this ->formatPrice ($ tierPrice [0 ], $ currencyCode );
322
+ }
323
+ }
231
324
232
325
/**
233
326
* @param $groupId
0 commit comments