Skip to content

Commit 0bcdcec

Browse files
ENGCOM-1389: MAGETWO-84124: Add bundle parent/child relationship during import #104
- Merge Pull Request magento-engcom/import-export-improvements#104 from adam-paterson/import-export-improvements:MAGETWO-84124-import-bundles-no-stock - Merged commits: 1. 9cbef5e 2. e07b074 3. f1657b8
2 parents bed17e0 + f1657b8 commit 0bcdcec

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-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+
private 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: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\BundleImportExport\Model\Import\Product\Type\Bundle;
77

8+
use Magento\Catalog\Model\ResourceModel\Product\Relation;
9+
use Magento\Framework\App\ObjectManager;
10+
811
/**
912
* A bundle product relations (options, selections, etc.) data saver.
1013
*
@@ -17,13 +20,22 @@ class RelationsDataSaver
1720
*/
1821
private $resource;
1922

23+
/**
24+
* @var Relation
25+
*/
26+
private $productRelation;
27+
2028
/**
2129
* @param \Magento\Framework\App\ResourceConnection $resource
30+
* @param Relation $productRelation
2231
*/
2332
public function __construct(
24-
\Magento\Framework\App\ResourceConnection $resource
33+
\Magento\Framework\App\ResourceConnection $resource,
34+
Relation $productRelation = null
2535
) {
26-
$this->resource = $resource;
36+
$this->resource = $resource;
37+
$this->productRelation = $productRelation
38+
?: ObjectManager::getInstance()->get(Relation::class);
2739
}
2840

2941
/**
@@ -92,4 +104,17 @@ public function saveSelections(array $selections)
92104
);
93105
}
94106
}
107+
108+
/**
109+
* Saves given parent/child relations.
110+
*
111+
* @param int $parentId
112+
* @param array $childIds
113+
*
114+
* @return void
115+
*/
116+
public function saveProductRelations($parentId, $childIds)
117+
{
118+
$this->productRelation->processRelations($parentId, $childIds);
119+
}
95120
}

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)