Skip to content

Commit 9cbef5e

Browse files
committed
MAGETWO-84124: Add bundle parent/child relationship during import
Resolves: #12330
1 parent ec06e89 commit 9cbef5e

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ public function saveData()
411411
$this->populateExistingOptions();
412412
$this->insertOptions();
413413
$this->insertSelections();
414+
$this->insertParentChildRelations();
414415
$this->clear();
415416
}
416417
}
@@ -659,6 +660,32 @@ protected function insertSelections()
659660
return $this;
660661
}
661662

663+
/**
664+
* Insert parent/child product relations
665+
*
666+
* @return \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType
667+
*/
668+
protected function insertParentChildRelations()
669+
{
670+
foreach ($this->_cachedOptions as $productId => $options) {
671+
$childIds = [];
672+
foreach ($options as $option) {
673+
foreach ($option['selections'] as $selection) {
674+
if (!isset($selection['parent_product_id'])) {
675+
if (!isset($this->_cachedSkuToProducts[$selection['sku']])) {
676+
continue;
677+
}
678+
$childIds[] = $this->_cachedSkuToProducts[$selection['sku']];
679+
}
680+
}
681+
682+
$this->relationsDataSaver->saveProductRelations($productId, $childIds);
683+
}
684+
}
685+
686+
return $this;
687+
}
688+
662689
/**
663690
* Initialize attributes parameters for all attributes' sets.
664691
*

app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle/RelationsDataSaver.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@ class RelationsDataSaver
1717
*/
1818
private $resource;
1919

20+
/**
21+
* @var \Magento\Catalog\Model\ResourceModel\Product\Relation
22+
*/
23+
private $productRelation;
24+
2025
/**
2126
* @param \Magento\Framework\App\ResourceConnection $resource
27+
* @param \Magento\Catalog\Model\ResourceModel\Product\Relation $productRelation
2228
*/
2329
public function __construct(
24-
\Magento\Framework\App\ResourceConnection $resource
30+
\Magento\Framework\App\ResourceConnection $resource,
31+
\Magento\Catalog\Model\ResourceModel\Product\Relation $productRelation
2532
) {
26-
$this->resource = $resource;
33+
$this->resource = $resource;
34+
$this->productRelation = $productRelation;
2735
}
2836

2937
/**
@@ -92,4 +100,17 @@ public function saveSelections(array $selections)
92100
);
93101
}
94102
}
103+
104+
/**
105+
* Saves given parent/child relations.
106+
*
107+
* @param int $parentId
108+
* @param array $childIds
109+
*
110+
* @return void
111+
*/
112+
public function saveProductRelations($parentId, $childIds)
113+
{
114+
$this->productRelation->processRelations($parentId, $childIds);
115+
}
95116
}

app/code/Magento/BundleImportExport/Test/Unit/Model/Import/Product/Type/Bundle/RelationsDataSaverTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\BundleImportExport\Test\Unit\Model\Import\Product\Type\Bundle;
88

99
use Magento\BundleImportExport\Model\Import\Product\Type\Bundle\RelationsDataSaver;
10+
use Magento\Catalog\Model\ResourceModel\Product\Relation;
1011
use Magento\Framework\App\ResourceConnection;
1112
use Magento\Framework\DB\Adapter\AdapterInterface;
1213

@@ -30,6 +31,11 @@ class RelationsDataSaverTest extends \PHPUnit\Framework\TestCase
3031
*/
3132
private $connectionMock;
3233

34+
/**
35+
* @var Relation|\PHPUnit_Framework_MockObject_MockObject
36+
*/
37+
private $productRelationMock;
38+
3339
protected function setUp()
3440
{
3541
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -39,12 +45,16 @@ protected function setUp()
3945
$this->connectionMock = $this->getMockBuilder(AdapterInterface::class)
4046
->disableOriginalConstructor()
4147
->getMock();
42-
$this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
48+
49+
$this->productRelationMock = $this->getMockBuilder(Relation::class)
50+
->disableOriginalConstructor()
51+
->getMock();
4352

4453
$this->relationsDataSaver = $helper->getObject(
4554
RelationsDataSaver::class,
4655
[
47-
'resource' => $this->resourceMock
56+
'resource' => $this->resourceMock,
57+
'productRelation' => $this->productRelationMock
4858
]
4959
);
5060
}
@@ -53,7 +63,7 @@ public function testSaveOptions()
5363
{
5464
$options = [1, 2];
5565
$table_name= 'catalog_product_bundle_option';
56-
66+
$this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
5767
$this->resourceMock->expects($this->once())
5868
->method('getTableName')
5969
->with('catalog_product_bundle_option')
@@ -78,6 +88,7 @@ public function testSaveOptionValues()
7888
$optionsValues = [1, 2];
7989
$table_name= 'catalog_product_bundle_option_value';
8090

91+
$this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
8192
$this->resourceMock->expects($this->once())
8293
->method('getTableName')
8394
->with('catalog_product_bundle_option_value')
@@ -98,6 +109,7 @@ public function testSaveSelections()
98109
$selections = [1, 2];
99110
$table_name= 'catalog_product_bundle_selection';
100111

112+
$this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock);
101113
$this->resourceMock->expects($this->once())
102114
->method('getTableName')
103115
->with('catalog_product_bundle_selection')
@@ -121,4 +133,16 @@ public function testSaveSelections()
121133

122134
$this->relationsDataSaver->saveSelections($selections);
123135
}
136+
137+
public function testSaveProductRelations()
138+
{
139+
$parentId = 1;
140+
$children = [2, 3];
141+
142+
$this->productRelationMock->expects($this->once())
143+
->method('processRelations')
144+
->with($parentId, $children);
145+
146+
$this->relationsDataSaver->saveProductRelations($parentId, $children);
147+
}
124148
}

0 commit comments

Comments
 (0)