Skip to content

Commit 3491d79

Browse files
committed
Merge remote-tracking branch 'tango/MC-21727' into Chaika-PR-2019-11-19
2 parents f07de01 + 89e601e commit 3491d79

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ protected function _initTypeModels()
11981198
// phpcs:disable Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
11991199
$this->_fieldsMap = array_merge($this->_fieldsMap, $model->getCustomFieldsMapping());
12001200
$this->_specialAttributes = array_merge($this->_specialAttributes, $model->getParticularAttributes());
1201-
// phpcs:enable
1201+
// phpcs:enable
12021202
}
12031203
$this->_initErrorTemplates();
12041204
// remove doubles
@@ -3060,6 +3060,8 @@ private function getValidationErrorLevel($sku): string
30603060
* @param int $nextLinkId
30613061
* @param array $positionAttrId
30623062
* @return void
3063+
* @throws LocalizedException
3064+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
30633065
*/
30643066
private function processLinkBunches(
30653067
array $bunch,
@@ -3070,6 +3072,7 @@ private function processLinkBunches(
30703072
$productIds = [];
30713073
$linkRows = [];
30723074
$positionRows = [];
3075+
$linksToDelete = [];
30733076

30743077
$bunch = array_filter($bunch, [$this, 'isRowAllowedToImport'], ARRAY_FILTER_USE_BOTH);
30753078
foreach ($bunch as $rowData) {
@@ -3086,10 +3089,15 @@ function ($linkName) use ($rowData) {
30863089
);
30873090
foreach ($linkNameToId as $linkName => $linkId) {
30883091
$linkSkus = explode($this->getMultipleValueSeparator(), $rowData[$linkName . 'sku']);
3092+
//process empty value
3093+
if (!empty($linkSkus[0]) && $linkSkus[0] === $this->getEmptyAttributeValueConstant()) {
3094+
$linksToDelete[$linkId][] = $productId;
3095+
continue;
3096+
}
3097+
30893098
$linkPositions = !empty($rowData[$linkName . 'position'])
30903099
? explode($this->getMultipleValueSeparator(), $rowData[$linkName . 'position'])
30913100
: [];
3092-
30933101
$linkSkus = array_filter(
30943102
$linkSkus,
30953103
function ($linkedSku) use ($sku) {
@@ -3098,6 +3106,7 @@ function ($linkedSku) use ($sku) {
30983106
&& strcasecmp($linkedSku, $sku) !== 0;
30993107
}
31003108
);
3109+
31013110
foreach ($linkSkus as $linkedKey => $linkedSku) {
31023111
$linkedId = $this->getProductLinkedId($linkedSku);
31033112
if ($linkedId == null) {
@@ -3129,9 +3138,34 @@ function ($linkedSku) use ($sku) {
31293138
}
31303139
}
31313140
}
3141+
$this->deleteProductsLinks($resource, $linksToDelete);
31323142
$this->saveLinksData($resource, $productIds, $linkRows, $positionRows);
31333143
}
31343144

3145+
/**
3146+
* Delete links
3147+
*
3148+
* @param Link $resource
3149+
* @param array $linksToDelete
3150+
* @return void
3151+
* @throws LocalizedException
3152+
*/
3153+
private function deleteProductsLinks(Link $resource, array $linksToDelete)
3154+
{
3155+
if (!empty($linksToDelete) && Import::BEHAVIOR_APPEND === $this->getBehavior()) {
3156+
foreach ($linksToDelete as $linkTypeId => $productIds) {
3157+
if (!empty($productIds)) {
3158+
$whereLinkId = $this->_connection->quoteInto('link_type_id', $linkTypeId);
3159+
$whereProductId = $this->_connection->quoteInto('product_id IN (?)', array_unique($productIds));
3160+
$this->_connection->delete(
3161+
$resource->getMainTable(),
3162+
$whereLinkId . ' AND ' . $whereProductId
3163+
);
3164+
}
3165+
}
3166+
}
3167+
}
3168+
31353169
/**
31363170
* Fetches Product Links
31373171
*

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
1717
use Magento\Catalog\Api\ProductRepositoryInterface;
1818
use Magento\Catalog\Model\Category;
19+
use Magento\Catalog\Model\Product;
20+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
1921
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
2022
use Magento\Framework\App\Bootstrap;
2123
use Magento\Framework\App\Filesystem\DirectoryList;
@@ -29,6 +31,7 @@
2931
use Magento\Store\Model\Store;
3032
use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection;
3133
use Psr\Log\LoggerInterface;
34+
use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
3235

3336
/**
3437
* Class ProductTest
@@ -312,7 +315,6 @@ public function testStockState()
312315
* @throws \Magento\Framework\Exception\LocalizedException
313316
* @throws \Magento\Framework\Exception\NoSuchEntityException
314317
* @magentoAppIsolation enabled
315-
316318
*
317319
* @return void
318320
*/
@@ -1574,6 +1576,49 @@ public function testValidateUrlKeysMultipleStores()
15741576
$this->assertTrue($errors->getErrorsCount() == 0);
15751577
}
15761578

1579+
/**
1580+
* @magentoDataFixture Magento/CatalogImportExport/_files/product_export_with_product_links_data.php
1581+
* @magentoAppArea adminhtml
1582+
* @magentoDbIsolation enabled
1583+
* @magentoAppIsolation enabled
1584+
*/
1585+
public function testProductLinksWithEmptyValue()
1586+
{
1587+
// import data from CSV file
1588+
$pathToFile = __DIR__ . '/_files/products_to_import_with_product_links_with_empty_value.csv';
1589+
$filesystem = BootstrapHelper::getObjectManager()->create(Filesystem::class);
1590+
1591+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
1592+
$source = $this->objectManager->create(
1593+
Csv::class,
1594+
[
1595+
'file' => $pathToFile,
1596+
'directory' => $directory
1597+
]
1598+
);
1599+
$errors = $this->_model->setSource(
1600+
$source
1601+
)->setParameters(
1602+
[
1603+
'behavior' => Import::BEHAVIOR_APPEND,
1604+
'entity' => 'catalog_product'
1605+
]
1606+
)->validateData();
1607+
1608+
$this->assertTrue($errors->getErrorsCount() == 0);
1609+
$this->_model->importData();
1610+
1611+
$objectManager = BootstrapHelper::getObjectManager();
1612+
$resource = $objectManager->get(ProductResource::class);
1613+
$productId = $resource->getIdBySku('simple');
1614+
/** @var \Magento\Catalog\Model\Product $product */
1615+
$product = BootstrapHelper::getObjectManager()->create(Product::class);
1616+
$product->load($productId);
1617+
1618+
$this->assertEmpty($product->getCrossSellProducts());
1619+
$this->assertEmpty($product->getUpSellProducts());
1620+
}
1621+
15771622
/**
15781623
* @magentoAppArea adminhtml
15791624
* @magentoDbIsolation enabled
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sku,crosssell_skus,crosssell_position,upsell_skus,upsell_position
2+
simple,__EMPTY__VALUE__,__EMPTY__VALUE__,__EMPTY__VALUE__,__EMPTY__VALUE__

0 commit comments

Comments
 (0)