@@ -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,70 +261,111 @@ 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
+ $ exportData = $ this ->correctExportData (
284
+ $ productsByStores ,
285
+ $ tierPricesData
286
+ );
287
+ if (!empty ($ exportData )) {
288
+ asort ($ exportData );
268
289
}
269
290
}
270
- if ($ exportData ) {
271
- $ exportData = $ this ->correctExportData ($ exportData );
272
- }
273
- if (isset ($ exportData )) {
274
- asort ($ exportData );
275
- }
276
291
} catch (\Exception $ e ) {
277
292
$ this ->_logger ->critical ($ e );
278
293
}
294
+
279
295
return $ exportData ;
280
296
}
281
297
282
298
/**
283
- * Correct export data .
299
+ * @param array $tierPriceData Tier price information .
284
300
*
285
- * @param array $exportData
286
- * @return array
287
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
301
+ * @return array Formatted for export tier price information.
288
302
*/
289
- protected function correctExportData ( $ exportData )
303
+ private function createExportRow ( array $ tierPriceData ): array
290
304
{
291
- $ customExportData = [];
292
- foreach ($ exportData as $ key => $ row ) {
293
- $ exportRow = $ this ->templateExportData ;
294
- foreach ($ exportRow as $ keyTemplate => $ valueTemplate ) {
295
- if (isset ($ row [$ keyTemplate ])) {
296
- if (in_array ($ keyTemplate , $ this ->_priceWebsite )) {
297
- $ exportRow [$ keyTemplate ] = $ this ->_getWebsiteCode (
298
- $ row [$ keyTemplate ]
299
- );
300
- } elseif (in_array ($ keyTemplate , $ this ->_priceCustomerGroup )) {
301
- $ exportRow [$ keyTemplate ] = $ this ->_getCustomerGroupById (
302
- $ row [$ keyTemplate ],
303
- isset ($ row [ImportAdvancedPricing::VALUE_ALL_GROUPS ])
304
- ? $ row [ImportAdvancedPricing::VALUE_ALL_GROUPS ]
305
- : null
306
- );
307
- unset($ exportRow [ImportAdvancedPricing::VALUE_ALL_GROUPS ]);
308
- } elseif ($ keyTemplate === ImportAdvancedPricing::COL_TIER_PRICE ) {
309
- $ exportRow [$ keyTemplate ] = $ row [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
310
- ? $ row [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
311
- : $ row [ImportAdvancedPricing::COL_TIER_PRICE ];
312
- $ exportRow [ImportAdvancedPricing::COL_TIER_PRICE_TYPE ]
313
- = $ this ->tierPriceTypeValue ($ row [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]);
314
- } else {
315
- $ exportRow [$ keyTemplate ] = $ row [$ keyTemplate ];
316
- }
305
+ $ exportRow = $ this ->templateExportData ;
306
+ foreach (array_keys ($ exportRow ) as $ keyTemplate ) {
307
+ if (array_key_exists ($ keyTemplate , $ tierPriceData )) {
308
+ if (in_array ($ keyTemplate , $ this ->_priceWebsite )) {
309
+ $ exportRow [$ keyTemplate ] = $ this ->_getWebsiteCode (
310
+ $ tierPriceData [$ keyTemplate ]
311
+ );
312
+ } elseif (in_array ($ keyTemplate , $ this ->_priceCustomerGroup )) {
313
+ $ exportRow [$ keyTemplate ] = $ this ->_getCustomerGroupById (
314
+ $ tierPriceData [$ keyTemplate ],
315
+ $ tierPriceData [ImportAdvancedPricing::VALUE_ALL_GROUPS ]
316
+ );
317
+ unset($ exportRow [ImportAdvancedPricing::VALUE_ALL_GROUPS ]);
318
+ } elseif ($ keyTemplate
319
+ === ImportAdvancedPricing::COL_TIER_PRICE
320
+ ) {
321
+ $ exportRow [$ keyTemplate ]
322
+ = $ tierPriceData [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
323
+ ? $ tierPriceData [ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE ]
324
+ : $ tierPriceData [ImportAdvancedPricing::COL_TIER_PRICE ];
325
+ $ exportRow [ImportAdvancedPricing::COL_TIER_PRICE_TYPE ]
326
+ = $ this ->tierPriceTypeValue ($ tierPriceData );
327
+ } else {
328
+ $ exportRow [$ keyTemplate ] = $ tierPriceData [$ keyTemplate ];
317
329
}
318
330
}
331
+ }
332
+
333
+ return $ exportRow ;
334
+ }
319
335
320
- $ customExportData [$ key ] = $ exportRow ;
321
- unset($ exportRow );
336
+ /**
337
+ * Correct export data.
338
+ *
339
+ * @param array $productsData
340
+ * @param array $tierPricesData
341
+ *
342
+ * @return array
343
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
344
+ */
345
+ protected function correctExportData (
346
+ array $ productsData ,
347
+ array $ tierPricesData
348
+ ): array {
349
+ //Assigning SKUs to tier prices data.
350
+ $ productLinkIdToSkuMap = [];
351
+ foreach ($ productsData as $ productData ) {
352
+ $ productLinkIdToSkuMap [$ productData [Store::DEFAULT_STORE_ID ][$ this ->getProductEntityLinkField ()]]
353
+ = $ productData [Store::DEFAULT_STORE_ID ]['sku ' ];
354
+ }
355
+ unset($ productData );
356
+ $ linkedTierPricesData = [];
357
+ foreach ($ tierPricesData as $ tierPriceData ) {
358
+ $ sku = $ productLinkIdToSkuMap [$ tierPriceData ['product_link_id ' ]];
359
+ $ linkedTierPricesData [] = array_merge (
360
+ $ tierPriceData ,
361
+ [ImportAdvancedPricing::COL_SKU => $ sku ]
362
+ );
363
+ }
364
+ unset($ sku , $ tierPriceData );
365
+
366
+ $ customExportData = [];
367
+ foreach ($ linkedTierPricesData as $ row ) {
368
+ $ customExportData [] = $ this ->createExportRow ($ row );
322
369
}
323
370
324
371
return $ customExportData ;
@@ -327,67 +374,66 @@ protected function correctExportData($exportData)
327
374
/**
328
375
* Check type for tier price.
329
376
*
330
- * @param string $tierPricePercentage
377
+ * @param array $tierPriceData
378
+ *
331
379
* @return string
332
380
*/
333
- private function tierPriceTypeValue ($ tierPricePercentage )
381
+ private function tierPriceTypeValue (array $ tierPriceData ): string
334
382
{
335
- return $ tierPricePercentage
383
+ return $ tierPriceData [ImportAdvancedPricing:: COL_TIER_PRICE_PERCENTAGE_VALUE ]
336
384
? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
337
385
: ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED ;
338
386
}
339
387
340
388
/**
341
389
* Get tier prices.
342
390
*
343
- * @param array $listSku
391
+ * @param string[] $productLinksIds
344
392
* @param string $table
345
393
* @return array|bool
346
394
* @SuppressWarnings(PHPMD.NPathComplexity)
347
395
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
348
396
*/
349
- protected function getTierPrices (array $ listSku , $ table )
397
+ protected function getTierPrices (array $ productLinksIds , $ table )
350
398
{
399
+ $ exportFilter = null ;
400
+ $ price = null ;
351
401
if (isset ($ this ->_parameters [\Magento \ImportExport \Model \Export::FILTER_ELEMENT_GROUP ])) {
352
402
$ exportFilter = $ this ->_parameters [\Magento \ImportExport \Model \Export::FILTER_ELEMENT_GROUP ];
353
403
}
404
+ $ productEntityLinkField = $ this ->getProductEntityLinkField ();
405
+
354
406
if ($ table == ImportAdvancedPricing::TABLE_TIER_PRICE ) {
355
407
$ 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 ' ,
408
+ ImportAdvancedPricing::COL_TIER_PRICE_WEBSITE => 'ap.website_id ' ,
409
+ ImportAdvancedPricing::VALUE_ALL_GROUPS => 'ap.all_groups ' ,
410
+ ImportAdvancedPricing::COL_TIER_PRICE_CUSTOMER_GROUP => 'ap.customer_group_id ' ,
411
+ ImportAdvancedPricing::COL_TIER_PRICE_QTY => 'ap.qty ' ,
412
+ ImportAdvancedPricing::COL_TIER_PRICE => 'ap.value ' ,
362
413
ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value ' ,
414
+ 'product_link_id ' => 'ap. '
415
+ .$ productEntityLinkField ,
363
416
];
364
- if (isset ($ exportFilter ) && !empty ($ exportFilter )) {
365
- $ price = $ exportFilter ['tier_price ' ];
366
- }
367
- }
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 ' );
417
+ if ($ exportFilter ) {
418
+ if (array_key_exists ('tier_price ' , $ exportFilter )) {
419
+ $ price = $ exportFilter ['tier_price ' ];
376
420
}
377
421
}
422
+ } else {
423
+ throw new \InvalidArgumentException ('Proper table name needed ' );
424
+ }
425
+
426
+ if ($ productLinksIds ) {
378
427
try {
379
- $ productEntityLinkField = $ this ->getProductEntityLinkField ();
380
428
$ select = $ this ->_connection ->select ()
381
429
->from (
382
- ['cpe ' => $ this ->_resource ->getTableName ('catalog_product_entity ' )],
383
- $ selectFields
384
- )
385
- ->joinInner (
386
430
['ap ' => $ this ->_resource ->getTableName ($ table )],
387
- 'ap. ' . $ productEntityLinkField . ' = cpe. ' . $ productEntityLinkField ,
388
- []
431
+ $ selectFields
389
432
)
390
- ->where ('cpe.entity_id IN (?) ' , $ listSku );
433
+ ->where (
434
+ 'ap. ' .$ productEntityLinkField .' IN (?) ' ,
435
+ $ productLinksIds
436
+ );
391
437
392
438
if (isset ($ price [0 ]) && !empty ($ price [0 ])) {
393
439
$ select ->where ('ap.value >= ? ' , $ price [0 ]);
@@ -398,18 +444,16 @@ protected function getTierPrices(array $listSku, $table)
398
444
if (isset ($ price [0 ]) && !empty ($ price [0 ]) || isset ($ price [1 ]) && !empty ($ price [1 ])) {
399
445
$ select ->orWhere ('ap.percentage_value IS NOT NULL ' );
400
446
}
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
- }
447
+
407
448
$ exportData = $ this ->_connection ->fetchAll ($ select );
408
449
} catch (\Exception $ e ) {
409
450
return false ;
410
451
}
452
+
453
+ return $ exportData ;
454
+ } else {
455
+ return false ;
411
456
}
412
- return $ exportData ;
413
457
}
414
458
415
459
/**
@@ -418,35 +462,46 @@ protected function getTierPrices(array $listSku, $table)
418
462
* @param int $websiteId
419
463
* @return string
420
464
*/
421
- protected function _getWebsiteCode ($ websiteId )
465
+ protected function _getWebsiteCode (int $ websiteId ): string
422
466
{
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 ;
467
+ if (!array_key_exists ($ websiteId , $ this ->websiteCodesMap )) {
468
+ $ storeName = ($ websiteId == 0 )
469
+ ? ImportAdvancedPricing::VALUE_ALL_WEBSITES
470
+ : $ this ->_storeManager ->getWebsite ($ websiteId )->getCode ();
471
+ $ currencyCode = '' ;
472
+ if ($ websiteId == 0 ) {
473
+ $ currencyCode = $ this ->_storeManager ->getWebsite ($ websiteId )
474
+ ->getBaseCurrencyCode ();
475
+ }
476
+
477
+ if ($ storeName && $ currencyCode ) {
478
+ $ code = $ storeName .' [ ' .$ currencyCode .'] ' ;
479
+ } else {
480
+ $ code = $ storeName ;
481
+ }
482
+ $ this ->websiteCodesMap [$ websiteId ] = $ code ;
434
483
}
484
+
485
+ return $ this ->websiteCodesMap [$ websiteId ];
435
486
}
436
487
437
488
/**
438
489
* Get Customer Group By Id
439
490
*
440
491
* @param int $customerGroupId
441
- * @param null $allGroups
492
+ * @param int $allGroups
442
493
* @return string
443
494
*/
444
- protected function _getCustomerGroupById ($ customerGroupId , $ allGroups = null )
445
- {
446
- if ($ allGroups ) {
495
+ protected function _getCustomerGroupById (
496
+ int $ customerGroupId ,
497
+ int $ allGroups = 0
498
+ ): string {
499
+ if ($ allGroups !== 0 ) {
447
500
return ImportAdvancedPricing::VALUE_ALL_GROUPS ;
448
501
} else {
449
- return $ this ->_groupRepository ->getById ($ customerGroupId )->getCode ();
502
+ return $ this ->_groupRepository
503
+ ->getById ($ customerGroupId )
504
+ ->getCode ();
450
505
}
451
506
}
452
507
0 commit comments