Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 2d2abca

Browse files
author
Oleksandr Gorkun
committed
MAGETWO-71549: Impossible to export Advanced Prices on a medium profile
- possible fix
1 parent 540f467 commit 2d2abca

File tree

2 files changed

+145
-82
lines changed

2 files changed

+145
-82
lines changed

app/code/Magento/AdvancedPricingImportExport/Model/Export/AdvancedPricing.php

Lines changed: 114 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ class AdvancedPricing extends \Magento\CatalogImportExport\Model\Export\Product
7979
ImportAdvancedPricing::COL_TIER_PRICE_TYPE => ''
8080
];
8181

82+
/**
83+
* @var string[]
84+
*/
85+
private $websiteCodesMap = [];
86+
8287
/**
8388
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
8489
* @param \Magento\Eav\Model\Config $config
@@ -213,6 +218,7 @@ public function export()
213218
break;
214219
}
215220
}
221+
216222
return $writer->getContents();
217223
}
218224

@@ -255,41 +261,73 @@ public function filterAttributeCollection(\Magento\Eav\Model\ResourceModel\Entit
255261
*/
256262
protected function getExportData()
257263
{
264+
if ($this->_passTierPrice) {
265+
return [];
266+
}
267+
258268
$exportData = [];
259269
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);
268290
}
269291
}
270-
if ($exportData) {
271-
$exportData = $this->correctExportData($exportData);
272-
}
273-
if (isset($exportData)) {
274-
asort($exportData);
275-
}
276292
} catch (\Exception $e) {
277293
$this->_logger->critical($e);
278294
}
295+
279296
return $exportData;
280297
}
281298

282299
/**
283300
* Correct export data.
284301
*
285-
* @param array $exportData
302+
* @param array $productsData
303+
* @param array $tierPricesData
304+
*
286305
* @return array
287306
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
288307
*/
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+
291329
$customExportData = [];
292-
foreach ($exportData as $key => $row) {
330+
foreach ($linkedTierPricesData as $row) {
293331
$exportRow = $this->templateExportData;
294332
foreach ($exportRow as $keyTemplate => $valueTemplate) {
295333
if (isset($row[$keyTemplate])) {
@@ -300,24 +338,22 @@ protected function correctExportData($exportData)
300338
} elseif (in_array($keyTemplate, $this->_priceCustomerGroup)) {
301339
$exportRow[$keyTemplate] = $this->_getCustomerGroupById(
302340
$row[$keyTemplate],
303-
isset($row[ImportAdvancedPricing::VALUE_ALL_GROUPS])
304-
? $row[ImportAdvancedPricing::VALUE_ALL_GROUPS]
305-
: null
341+
$row[ImportAdvancedPricing::VALUE_ALL_GROUPS]
306342
);
307343
unset($exportRow[ImportAdvancedPricing::VALUE_ALL_GROUPS]);
308344
} elseif ($keyTemplate === ImportAdvancedPricing::COL_TIER_PRICE) {
309345
$exportRow[$keyTemplate] = $row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
310346
? $row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
311347
: $row[ImportAdvancedPricing::COL_TIER_PRICE];
312348
$exportRow[ImportAdvancedPricing::COL_TIER_PRICE_TYPE]
313-
= $this->tierPriceTypeValue($row[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]);
349+
= $this->tierPriceTypeValue($row);
314350
} else {
315351
$exportRow[$keyTemplate] = $row[$keyTemplate];
316352
}
317353
}
318354
}
319355

320-
$customExportData[$key] = $exportRow;
356+
$customExportData[] = $exportRow;
321357
unset($exportRow);
322358
}
323359

@@ -327,67 +363,61 @@ protected function correctExportData($exportData)
327363
/**
328364
* Check type for tier price.
329365
*
330-
* @param string $tierPricePercentage
366+
* @param array $tierPriceData
367+
*
331368
* @return string
332369
*/
333-
private function tierPriceTypeValue($tierPricePercentage)
370+
private function tierPriceTypeValue(array $tierPriceData): string
334371
{
335-
return $tierPricePercentage
372+
return $tierPriceData[ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE]
336373
? ImportAdvancedPricing::TIER_PRICE_TYPE_PERCENT
337374
: ImportAdvancedPricing::TIER_PRICE_TYPE_FIXED;
338375
}
339376

340377
/**
341378
* Get tier prices.
342379
*
343-
* @param array $listSku
380+
* @param string[] $productLinksIds
344381
* @param string $table
345382
* @return array|bool
346383
* @SuppressWarnings(PHPMD.NPathComplexity)
347384
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
348385
*/
349-
protected function getTierPrices(array $listSku, $table)
386+
protected function getTierPrices(array $productLinksIds, $table)
350387
{
388+
$exportFilter = null;
351389
if (isset($this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP])) {
352390
$exportFilter = $this->_parameters[\Magento\ImportExport\Model\Export::FILTER_ELEMENT_GROUP];
353391
}
392+
$productEntityLinkField = $this->getProductEntityLinkField();
393+
354394
if ($table == ImportAdvancedPricing::TABLE_TIER_PRICE) {
355395
$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',
362401
ImportAdvancedPricing::COL_TIER_PRICE_PERCENTAGE_VALUE => 'ap.percentage_value',
402+
'product_link_id' => 'ap.'
403+
.$productEntityLinkField,
363404
];
364405
if (isset($exportFilter) && !empty($exportFilter)) {
365406
$price = $exportFilter['tier_price'];
366407
}
408+
} else {
409+
throw new \InvalidArgumentException('Proper table name needed');
367410
}
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) {
378413
try {
379-
$productEntityLinkField = $this->getProductEntityLinkField();
380414
$select = $this->_connection->select()
381415
->from(
382-
['cpe' => $this->_resource->getTableName('catalog_product_entity')],
383-
$selectFields
384-
)
385-
->joinInner(
386416
['ap' => $this->_resource->getTableName($table)],
387-
'ap.' . $productEntityLinkField . ' = cpe.' . $productEntityLinkField,
388-
[]
417+
$selectFields
389418
)
390-
->where('cpe.entity_id IN (?)', $listSku);
419+
->where('ap.'.$productEntityLinkField.' IN (?)',
420+
$productLinksIds);
391421

392422
if (isset($price[0]) && !empty($price[0])) {
393423
$select->where('ap.value >= ?', $price[0]);
@@ -398,18 +428,16 @@ protected function getTierPrices(array $listSku, $table)
398428
if (isset($price[0]) && !empty($price[0]) || isset($price[1]) && !empty($price[1])) {
399429
$select->orWhere('ap.percentage_value IS NOT NULL');
400430
}
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+
407432
$exportData = $this->_connection->fetchAll($select);
408433
} catch (\Exception $e) {
409434
return false;
410435
}
436+
437+
return $exportData;
438+
} else {
439+
return false;
411440
}
412-
return $exportData;
413441
}
414442

415443
/**
@@ -420,33 +448,44 @@ protected function getTierPrices(array $listSku, $table)
420448
*/
421449
protected function _getWebsiteCode($websiteId)
422450
{
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;
434467
}
468+
469+
return $this->websiteCodesMap[$websiteId];
435470
}
436471

437472
/**
438473
* Get Customer Group By Id
439474
*
440475
* @param int $customerGroupId
441-
* @param null $allGroups
476+
* @param int $allGroups
442477
* @return string
443478
*/
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) {
447484
return ImportAdvancedPricing::VALUE_ALL_GROUPS;
448485
} else {
449-
return $this->_groupRepository->getById($customerGroupId)->getCode();
486+
return $this->_groupRepository
487+
->getById($customerGroupId)
488+
->getCode();
450489
}
451490
}
452491

app/code/Magento/CatalogImportExport/Model/Export/Product.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Magento\ImportExport\Model\Import;
99
use \Magento\Store\Model\Store;
1010
use \Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
11+
use Magento\Catalog\Model\Product as ProductEntity;
1112

1213
/**
1314
* Export entity product model
@@ -913,6 +914,29 @@ protected function getExportData()
913914
return $exportData;
914915
}
915916

917+
/**
918+
* Load products' data from the collection
919+
* and filter it (if needed).
920+
*
921+
* @return array Keys are product IDs, values arrays with keys as store IDs
922+
* and values as store-specific versions of Product entity.
923+
*/
924+
protected function loadCollection(): array
925+
{
926+
$data = [];
927+
928+
$collection = $this->_getEntityCollection();
929+
foreach (array_keys($this->_storeIdToCode) as $storeId) {
930+
$collection->setStoreId($storeId);
931+
foreach ($collection as $itemId => $item) {
932+
$data[$itemId][$storeId] = $item;
933+
}
934+
}
935+
$collection->clear();
936+
937+
return $data;
938+
}
939+
916940
/**
917941
* Collect export data for all products
918942
*
@@ -923,14 +947,15 @@ protected function getExportData()
923947
protected function collectRawData()
924948
{
925949
$data = [];
926-
$collection = $this->_getEntityCollection();
927-
foreach ($this->_storeIdToCode as $storeId => $storeCode) {
928-
$collection->setStoreId($storeId);
950+
$items = $this->loadCollection();
951+
952+
foreach ($items as $itemId => $itemByStore) {
929953
/**
930-
* @var int $itemId
931-
* @var \Magento\Catalog\Model\Product $item
954+
* @var int $itemId
955+
* @var ProductEntity $item
932956
*/
933-
foreach ($collection as $itemId => $item) {
957+
foreach ($this->_storeIdToCode as $storeId => $storeCode) {
958+
$item = $itemByStore[$storeId];
934959
$additionalAttributes = [];
935960
$productLinkId = $item->getData($this->getProductEntityLinkField());
936961
foreach ($this->_getExportAttrCodes() as $code) {
@@ -1010,7 +1035,6 @@ protected function collectRawData()
10101035
$data[$itemId][$storeId]['product_id'] = $itemId;
10111036
$data[$itemId][$storeId]['product_link_id'] = $productLinkId;
10121037
}
1013-
$collection->clear();
10141038
}
10151039

10161040
return $data;

0 commit comments

Comments
 (0)