@@ -79,6 +79,11 @@ class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
79
79
ImportAdvancedPricing::COL_TIER_PRICE_TYPE => ''
80
80
];
81
81
82
+ /**
83
+ * @var string[]
84
+ */
85
+ private $ websiteCodesMap = [];
86
+
82
87
/**
83
88
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
84
89
* @param \Magento\Eav\Model\Config $config
@@ -213,6 +218,7 @@ public function export()
213
218
break ;
214
219
}
215
220
}
221
+
216
222
return $ writer ->getContents ();
217
223
}
218
224
@@ -255,41 +261,73 @@ public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entit
255
261
*/
256
262
protected function getExportData ()
257
263
{
264
+ if ($ this ->_passTierPrice ) {
265
+ return [];
266
+ }
267
+
258
268
$ exportData = [];
259
269
try {
260
- $ rawData = $ this ->collectRawData ();
261
- $ productIds = array_keys ($ rawData );
262
- if (isset ($ productIds )) {
263
- if (!$ this ->_passTierPrice ) {
264
- $ exportData = array_merge (
265
- $ exportData ,
266
- $ this ->getTierPrices ($ productIds , ImportAdvancedPricing::TABLE_TIER_PRICE )
267
- );
270
+ $ productsByStores = $ this ->loadCollection ();
271
+ if (!empty ($ productsByStores )) {
272
+ $ productLinkIds = array_map (
273
+ function (array $ productData ) {
274
+ return $ productData [Store::DEFAULT_STORE_ID ][$ this ->getProductEntityLinkField ()];
275
+ },
276
+ $ productsByStores
277
+ );
278
+ $ tierPricesData = $ this ->getTierPrices (
279
+ $ productLinkIds ,
280
+ ImportAdvancedPricing::TABLE_TIER_PRICE
281
+ );
282
+
283
+
284
+ $ exportData = $ this ->correctExportData (
285
+ $ productsByStores ,
286
+ $ tierPricesData
287
+ );
288
+ if (!empty ($ exportData )) {
289
+ asort ($ exportData );
268
290
}
269
291
}
270
- if ($ exportData ) {
271
- $ exportData = $ this ->correctExportData ($ exportData );
272
- }
273
- if (isset ($ exportData )) {
274
- asort ($ exportData );
275
- }
276
292
} catch (\Exception $ e ) {
277
293
$ this ->_logger ->critical ($ e );
278
294
}
295
+
279
296
return $ exportData ;
280
297
}
281
298
282
299
/**
283
300
* Correct export data.
284
301
*
285
- * @param array $exportData
302
+ * @param array $productsData
303
+ * @param array $tierPricesData
304
+ *
286
305
* @return array
287
306
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
288
307
*/
289
- protected function correctExportData ($ exportData )
290
- {
308
+ protected function correctExportData (
309
+ array $ productsData ,
310
+ array $ tierPricesData
311
+ ): array {
312
+ //Assigning SKUs to tier prices data.
313
+ $ productLinkIdToSkuMap = [];
314
+ foreach ($ productsData as $ productData ) {
315
+ $ productLinkIdToSkuMap [$ productData [Store::DEFAULT_STORE_ID ][$ this ->getProductEntityLinkField ()]]
316
+ = $ productData [Store::DEFAULT_STORE_ID ]['sku ' ];
317
+ }
318
+ unset($ productData );
319
+ $ linkedTierPricesData = [];
320
+ foreach ($ tierPricesData as $ tierPriceData ) {
321
+ $ sku = $ productLinkIdToSkuMap [$ tierPriceData ['product_link_id ' ]];
322
+ $ linkedTierPricesData [] = array_merge (
323
+ $ tierPriceData ,
324
+ [ImportAdvancedPricing::COL_SKU => $ sku ]
325
+ );
326
+ }
327
+ unset($ sku , $ tierPriceData );
328
+
291
329
$ customExportData = [];
292
- foreach ($ exportData as $ key => $ row ) {
330
+ foreach ($ linkedTierPricesData as $ row ) {
293
331
$ exportRow = $ this ->templateExportData ;
294
332
foreach ($ exportRow as $ keyTemplate => $ valueTemplate ) {
295
333
if (isset ($ row [$ keyTemplate ])) {
@@ -300,24 +338,22 @@ protected function correctExportData($exportData)
300
338
} elseif (in_array ($ keyTemplate , $ this ->_priceCustomerGroup )) {
301
339
$ exportRow [$ keyTemplate ] = $ this ->_getCustomerGroupById (
302
340
$ row [$ keyTemplate ],
303
- isset ($ row [ImportAdvancedPricing::VALUE_ALL_GROUPS ])
304
- ? $ row [ImportAdvancedPricing::VALUE_ALL_GROUPS ]
305
- : null
341
+ $ row [ImportAdvancedPricing::VALUE_ALL_GROUPS ]
306
342
);
307
343
unset($ exportRow [ImportAdvancedPricing::VALUE_ALL_GROUPS ]);
308
344
} elseif ($ keyTemplate === ImportAdvancedPricing::COL_TIER_PRICE ) {
309
345
$ exportRow [$ keyTemplate ] = $ row [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
310
346
? $ row [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
311
347
: $ row [ImportAdvancedPricing::COL_TIER_PRICE ];
312
348
$ exportRow [ImportAdvancedPricing::COL_TIER_PRICE_TYPE ]
313
- = $ this ->tierPriceTypeValue ($ row[ImportAdvancedPricing:: COL_TIER_PRICE_PERCENTAGE_VALUE ] );
349
+ = $ this ->tierPriceTypeValue ($ row );
314
350
} else {
315
351
$ exportRow [$ keyTemplate ] = $ row [$ keyTemplate ];
316
352
}
317
353
}
318
354
}
319
355
320
- $ customExportData [$ key ] = $ exportRow ;
356
+ $ customExportData [] = $ exportRow ;
321
357
unset($ exportRow );
322
358
}
323
359
@@ -327,67 +363,61 @@ protected function correctExportData($exportData)
327
363
/**
328
364
* Check type for tier price.
329
365
*
330
- * @param string $tierPricePercentage
366
+ * @param array $tierPriceData
367
+ *
331
368
* @return string
332
369
*/
333
- private function tierPriceTypeValue ($ tierPricePercentage )
370
+ private function tierPriceTypeValue (array $ tierPriceData ): string
334
371
{
335
- return $ tierPricePercentage
372
+ return $ tierPriceData [ImportAdvancedPricing:: COL_TIER_PRICE_PERCENTAGE_VALUE ]
336
373
? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
337
374
: ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED ;
338
375
}
339
376
340
377
/**
341
378
* Get tier prices.
342
379
*
343
- * @param array $listSku
380
+ * @param string[] $productLinksIds
344
381
* @param string $table
345
382
* @return array|bool
346
383
* @SuppressWarnings(PHPMD.NPathComplexity)
347
384
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
348
385
*/
349
- protected function getTierPrices (array $ listSku , $ table )
386
+ protected function getTierPrices (array $ productLinksIds , $ table )
350
387
{
388
+ $ exportFilter = null ;
351
389
if (isset ($ this ->_parameters [\Magento \ImportExport \Model \Export::FILTER_ELEMENT_GROUP ])) {
352
390
$ exportFilter = $ this ->_parameters [\Magento \ImportExport \Model \Export::FILTER_ELEMENT_GROUP ];
353
391
}
392
+ $ productEntityLinkField = $ this ->getProductEntityLinkField ();
393
+
354
394
if ($ table == ImportAdvancedPricing::TABLE_TIER_PRICE ) {
355
395
$ selectFields = [
356
- ImportAdvancedPricing::COL_SKU => 'cpe.sku ' ,
357
- ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id ' ,
358
- ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups ' ,
359
- ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id ' ,
360
- ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty ' ,
361
- ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value ' ,
396
+ ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id ' ,
397
+ ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups ' ,
398
+ ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id ' ,
399
+ ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty ' ,
400
+ ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value ' ,
362
401
ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value ' ,
402
+ 'product_link_id ' => 'ap. '
403
+ .$ productEntityLinkField ,
363
404
];
364
405
if (isset ($ exportFilter ) && !empty ($ exportFilter )) {
365
406
$ price = $ exportFilter ['tier_price ' ];
366
407
}
408
+ } else {
409
+ throw new \InvalidArgumentException ('Proper table name needed ' );
367
410
}
368
- if ($ listSku ) {
369
- if (isset ($ exportFilter ) && !empty ($ exportFilter )) {
370
- $ date = $ exportFilter [\Magento \Catalog \Model \Category::KEY_UPDATED_AT ];
371
- if (isset ($ date [0 ]) && !empty ($ date [0 ])) {
372
- $ updatedAtFrom = $ this ->_localeDate ->date ($ date [0 ], null , false )->format ('Y-m-d H:i:s ' );
373
- }
374
- if (isset ($ date [1 ]) && !empty ($ date [1 ])) {
375
- $ updatedAtTo = $ this ->_localeDate ->date ($ date [1 ], null , false )->format ('Y-m-d H:i:s ' );
376
- }
377
- }
411
+
412
+ if ($ productLinksIds ) {
378
413
try {
379
- $ productEntityLinkField = $ this ->getProductEntityLinkField ();
380
414
$ select = $ this ->_connection ->select ()
381
415
->from (
382
- ['cpe ' => $ this ->_resource ->getTableName ('catalog_product_entity ' )],
383
- $ selectFields
384
- )
385
- ->joinInner (
386
416
['ap ' => $ this ->_resource ->getTableName ($ table )],
387
- 'ap. ' . $ productEntityLinkField . ' = cpe. ' . $ productEntityLinkField ,
388
- []
417
+ $ selectFields
389
418
)
390
- ->where ('cpe.entity_id IN (?) ' , $ listSku );
419
+ ->where ('ap. ' .$ productEntityLinkField .' IN (?) ' ,
420
+ $ productLinksIds );
391
421
392
422
if (isset ($ price [0 ]) && !empty ($ price [0 ])) {
393
423
$ select ->where ('ap.value >= ? ' , $ price [0 ]);
@@ -398,18 +428,16 @@ protected function getTierPrices(array $listSku, $table)
398
428
if (isset ($ price [0 ]) && !empty ($ price [0 ]) || isset ($ price [1 ]) && !empty ($ price [1 ])) {
399
429
$ select ->orWhere ('ap.percentage_value IS NOT NULL ' );
400
430
}
401
- if (isset ($ updatedAtFrom ) && !empty ($ updatedAtFrom )) {
402
- $ select ->where ('cpe.updated_at >= ? ' , $ updatedAtFrom );
403
- }
404
- if (isset ($ updatedAtTo ) && !empty ($ updatedAtTo )) {
405
- $ select ->where ('cpe.updated_at <= ? ' , $ updatedAtTo );
406
- }
431
+
407
432
$ exportData = $ this ->_connection ->fetchAll ($ select );
408
433
} catch (\Exception $ e ) {
409
434
return false ;
410
435
}
436
+
437
+ return $ exportData ;
438
+ } else {
439
+ return false ;
411
440
}
412
- return $ exportData ;
413
441
}
414
442
415
443
/**
@@ -420,33 +448,44 @@ protected function getTierPrices(array $listSku, $table)
420
448
*/
421
449
protected function _getWebsiteCode ($ websiteId )
422
450
{
423
- $ storeName = ($ websiteId == 0 )
424
- ? ImportAdvancedPricing::VALUE_ALL_WEBSITES
425
- : $ this ->_storeManager ->getWebsite ($ websiteId )->getCode ();
426
- $ currencyCode = '' ;
427
- if ($ websiteId == 0 ) {
428
- $ currencyCode = $ this ->_storeManager ->getWebsite ($ websiteId )->getBaseCurrencyCode ();
429
- }
430
- if ($ storeName && $ currencyCode ) {
431
- return $ storeName . ' [ ' . $ currencyCode . '] ' ;
432
- } else {
433
- return $ storeName ;
451
+ if (!array_key_exists ($ websiteId , $ this ->websiteCodesMap )) {
452
+ $ storeName = ($ websiteId == 0 )
453
+ ? ImportAdvancedPricing::VALUE_ALL_WEBSITES
454
+ : $ this ->_storeManager ->getWebsite ($ websiteId )->getCode ();
455
+ $ currencyCode = '' ;
456
+ if ($ websiteId == 0 ) {
457
+ $ currencyCode = $ this ->_storeManager ->getWebsite ($ websiteId )
458
+ ->getBaseCurrencyCode ();
459
+ }
460
+
461
+ if ($ storeName && $ currencyCode ) {
462
+ $ code = $ storeName .' [ ' .$ currencyCode .'] ' ;
463
+ } else {
464
+ $ code = $ storeName ;
465
+ }
466
+ $ this ->websiteCodesMap [$ websiteId ] = $ code ;
434
467
}
468
+
469
+ return $ this ->websiteCodesMap [$ websiteId ];
435
470
}
436
471
437
472
/**
438
473
* Get Customer Group By Id
439
474
*
440
475
* @param int $customerGroupId
441
- * @param null $allGroups
476
+ * @param int $allGroups
442
477
* @return string
443
478
*/
444
- protected function _getCustomerGroupById ($ customerGroupId , $ allGroups = null )
445
- {
446
- if ($ allGroups ) {
479
+ protected function _getCustomerGroupById (
480
+ int $ customerGroupId ,
481
+ int $ allGroups = 0
482
+ ): string {
483
+ if ($ allGroups !== 0 ) {
447
484
return ImportAdvancedPricing::VALUE_ALL_GROUPS ;
448
485
} else {
449
- return $ this ->_groupRepository ->getById ($ customerGroupId )->getCode ();
486
+ return $ this ->_groupRepository
487
+ ->getById ($ customerGroupId )
488
+ ->getCode ();
450
489
}
451
490
}
452
491
0 commit comments