15
15
*
16
16
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
17
17
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18
+ * @SuppressWarnings(PHPMD.TooManyFields)
18
19
*/
19
20
class AdvancedPricing extends \Magento \ImportExport \Model \Import \Entity \AbstractEntity
20
21
{
@@ -126,7 +127,7 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
126
127
/**
127
128
* @var array
128
129
*/
129
- protected $ _oldSkus ;
130
+ protected $ _oldSkus = null ;
130
131
131
132
/**
132
133
* Permanent entity columns.
@@ -147,6 +148,13 @@ class AdvancedPricing extends \Magento\ImportExport\Model\Import\Entity\Abstract
147
148
*/
148
149
protected $ dateTime ;
149
150
151
+ /**
152
+ * Product entity link field
153
+ *
154
+ * @var string
155
+ */
156
+ private $ productEntityLinkField ;
157
+
150
158
/**
151
159
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
152
160
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
@@ -199,11 +207,11 @@ public function __construct(
199
207
$ this ->_storeResolver = $ storeResolver ;
200
208
$ this ->_importProduct = $ importProduct ;
201
209
$ this ->_validators [self ::VALIDATOR_MAIN ] = $ validator ->init ($ this );
210
+ $ this ->_catalogProductEntity = $ this ->_resourceFactory ->create ()->getTable ('catalog_product_entity ' );
202
211
$ this ->_oldSkus = $ this ->retrieveOldSkus ();
203
212
$ this ->_validators [self ::VALIDATOR_WEBSITE ] = $ websiteValidator ;
204
213
$ this ->_validators [self ::VALIDATOR_TEAR_PRICE ] = $ tierPriceValidator ;
205
214
$ this ->errorAggregator = $ errorAggregator ;
206
- $ this ->_catalogProductEntity = $ this ->_resourceFactory ->create ()->getTable ('catalog_product_entity ' );
207
215
208
216
foreach (array_merge ($ this ->errorMessageTemplates , $ this ->_messageTemplates ) as $ errorCode => $ message ) {
209
217
$ this ->getErrorAggregator ()->addErrorMessageTemplate ($ errorCode , $ message );
@@ -409,11 +417,12 @@ protected function saveProductPrices(array $priceData, $table)
409
417
$ tableName = $ this ->_resourceFactory ->create ()->getTable ($ table );
410
418
$ priceIn = [];
411
419
$ entityIds = [];
420
+ $ oldSkus = $ this ->retrieveOldSkus ();
412
421
foreach ($ priceData as $ sku => $ priceRows ) {
413
- if (isset ($ this -> _oldSkus [$ sku ])) {
414
- $ productId = $ this -> _oldSkus [$ sku ];
422
+ if (isset ($ oldSkus [$ sku ])) {
423
+ $ productId = $ oldSkus [$ sku ];
415
424
foreach ($ priceRows as $ row ) {
416
- $ row [' entity_id ' ] = $ productId ;
425
+ $ row [$ this -> getProductEntityLinkField () ] = $ productId ;
417
426
$ priceIn [] = $ row ;
418
427
$ entityIds [] = $ productId ;
419
428
}
@@ -430,24 +439,26 @@ protected function saveProductPrices(array $priceData, $table)
430
439
* Deletes tier prices prices.
431
440
*
432
441
* @param array $listSku
433
- * @param string $tableName
434
- * @return bool
442
+ * @param string $table
443
+ * @return boolean
435
444
*/
436
- protected function deleteProductTierPrices (array $ listSku , $ tableName )
445
+ protected function deleteProductTierPrices (array $ listSku , $ table )
437
446
{
447
+ $ tableName = $ this ->_resourceFactory ->create ()->getTable ($ table );
448
+ $ productEntityLinkField = $ this ->getProductEntityLinkField ();
438
449
if ($ tableName && $ listSku ) {
439
450
if (!$ this ->_cachedSkuToDelete ) {
440
451
$ this ->_cachedSkuToDelete = $ this ->_connection ->fetchCol (
441
452
$ this ->_connection ->select ()
442
- ->from ($ this ->_catalogProductEntity , ' entity_id ' )
453
+ ->from ($ this ->_catalogProductEntity , $ productEntityLinkField )
443
454
->where ('sku IN (?) ' , $ listSku )
444
455
);
445
456
}
446
457
if ($ this ->_cachedSkuToDelete ) {
447
458
try {
448
459
$ this ->countItemsDeleted += $ this ->_connection ->delete (
449
460
$ tableName ,
450
- $ this ->_connection ->quoteInto (' entity_id IN (?) ' , $ this ->_cachedSkuToDelete )
461
+ $ this ->_connection ->quoteInto ($ productEntityLinkField . ' IN (?) ' , $ this ->_cachedSkuToDelete )
451
462
);
452
463
return true ;
453
464
} catch (\Exception $ e ) {
@@ -511,13 +522,15 @@ protected function getCustomerGroupId($customerGroup)
511
522
*/
512
523
protected function retrieveOldSkus ()
513
524
{
514
- $ oldSkus = $ this ->_connection ->fetchPairs (
515
- $ this ->_connection ->select ()->from (
516
- $ this ->_connection ->getTableName ('catalog_product_entity ' ),
517
- ['sku ' , 'entity_id ' ]
518
- )
519
- );
520
- return $ oldSkus ;
525
+ if ($ this ->_oldSkus === null ) {
526
+ $ this ->_oldSkus = $ this ->_connection ->fetchPairs (
527
+ $ this ->_connection ->select ()->from (
528
+ $ this ->_catalogProductEntity ,
529
+ ['sku ' , $ this ->getProductEntityLinkField ()]
530
+ )
531
+ );
532
+ }
533
+ return $ this ->_oldSkus ;
521
534
}
522
535
523
536
/**
@@ -529,15 +542,18 @@ protected function retrieveOldSkus()
529
542
*/
530
543
protected function processCountExistingPrices ($ prices , $ table )
531
544
{
545
+ $ tableName = $ this ->_resourceFactory ->create ()->getTable ($ table );
546
+ $ productEntityLinkField = $ this ->getProductEntityLinkField ();
532
547
$ existingPrices = $ this ->_connection ->fetchAssoc (
533
548
$ this ->_connection ->select ()->from (
534
- $ this -> _connection -> getTableName ( $ table ) ,
535
- ['value_id ' , ' entity_id ' , 'all_groups ' , 'customer_group_id ' ]
549
+ $ tableName ,
550
+ ['value_id ' , $ productEntityLinkField , 'all_groups ' , 'customer_group_id ' ]
536
551
)
537
552
);
553
+ $ oldSkus = $ this ->retrieveOldSkus ();
538
554
foreach ($ existingPrices as $ existingPrice ) {
539
- foreach ($ this -> _oldSkus as $ sku => $ productId ) {
540
- if ($ existingPrice [' entity_id ' ] == $ productId && isset ($ prices [$ sku ])) {
555
+ foreach ($ oldSkus as $ sku => $ productId ) {
556
+ if ($ existingPrice [$ productEntityLinkField ] == $ productId && isset ($ prices [$ sku ])) {
541
557
$ this ->incrementCounterUpdated ($ prices [$ sku ], $ existingPrice );
542
558
}
543
559
}
@@ -579,4 +595,19 @@ protected function processCountNewPrices(array $tierPrices)
579
595
580
596
return $ this ;
581
597
}
598
+
599
+ /**
600
+ * Get product entity link field
601
+ *
602
+ * @return string
603
+ */
604
+ private function getProductEntityLinkField ()
605
+ {
606
+ if (!$ this ->productEntityLinkField ) {
607
+ $ this ->productEntityLinkField = $ this ->getMetadataPool ()
608
+ ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class)
609
+ ->getLinkField ();
610
+ }
611
+ return $ this ->productEntityLinkField ;
612
+ }
582
613
}
0 commit comments