diff --git a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php index 1d2ac3a87ab77..3ed7e144ddd5a 100644 --- a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php +++ b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php @@ -411,6 +411,7 @@ public function saveData() $this->populateExistingOptions(); $this->insertOptions(); $this->insertSelections(); + $this->insertParentChildRelations(); $this->clear(); } } @@ -659,6 +660,32 @@ protected function insertSelections() return $this; } + /** + * Insert parent/child product relations + * + * @return \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType + */ + private function insertParentChildRelations() + { + foreach ($this->_cachedOptions as $productId => $options) { + $childIds = []; + foreach ($options as $option) { + foreach ($option['selections'] as $selection) { + if (!isset($selection['parent_product_id'])) { + if (!isset($this->_cachedSkuToProducts[$selection['sku']])) { + continue; + } + $childIds[] = $this->_cachedSkuToProducts[$selection['sku']]; + } + } + + $this->relationsDataSaver->saveProductRelations($productId, $childIds); + } + } + + return $this; + } + /** * Initialize attributes parameters for all attributes' sets. * diff --git a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle/RelationsDataSaver.php b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle/RelationsDataSaver.php index a58195f823bf1..5409d12ac56d3 100644 --- a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle/RelationsDataSaver.php +++ b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle/RelationsDataSaver.php @@ -5,6 +5,9 @@ */ namespace Magento\BundleImportExport\Model\Import\Product\Type\Bundle; +use Magento\Catalog\Model\ResourceModel\Product\Relation; +use Magento\Framework\App\ObjectManager; + /** * A bundle product relations (options, selections, etc.) data saver. * @@ -17,13 +20,22 @@ class RelationsDataSaver */ private $resource; + /** + * @var Relation + */ + private $productRelation; + /** * @param \Magento\Framework\App\ResourceConnection $resource + * @param Relation $productRelation */ public function __construct( - \Magento\Framework\App\ResourceConnection $resource + \Magento\Framework\App\ResourceConnection $resource, + Relation $productRelation = null ) { - $this->resource = $resource; + $this->resource = $resource; + $this->productRelation = $productRelation + ?: ObjectManager::getInstance()->get(Relation::class); } /** @@ -92,4 +104,17 @@ public function saveSelections(array $selections) ); } } + + /** + * Saves given parent/child relations. + * + * @param int $parentId + * @param array $childIds + * + * @return void + */ + public function saveProductRelations($parentId, $childIds) + { + $this->productRelation->processRelations($parentId, $childIds); + } } diff --git a/app/code/Magento/BundleImportExport/Test/Unit/Model/Import/Product/Type/Bundle/RelationsDataSaverTest.php b/app/code/Magento/BundleImportExport/Test/Unit/Model/Import/Product/Type/Bundle/RelationsDataSaverTest.php index 42d508cdfb195..d50243b3656f3 100644 --- a/app/code/Magento/BundleImportExport/Test/Unit/Model/Import/Product/Type/Bundle/RelationsDataSaverTest.php +++ b/app/code/Magento/BundleImportExport/Test/Unit/Model/Import/Product/Type/Bundle/RelationsDataSaverTest.php @@ -7,6 +7,7 @@ namespace Magento\BundleImportExport\Test\Unit\Model\Import\Product\Type\Bundle; use Magento\BundleImportExport\Model\Import\Product\Type\Bundle\RelationsDataSaver; +use Magento\Catalog\Model\ResourceModel\Product\Relation; use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\Adapter\AdapterInterface; @@ -30,6 +31,11 @@ class RelationsDataSaverTest extends \PHPUnit\Framework\TestCase */ private $connectionMock; + /** + * @var Relation|\PHPUnit_Framework_MockObject_MockObject + */ + private $productRelationMock; + protected function setUp() { $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); @@ -39,12 +45,16 @@ protected function setUp() $this->connectionMock = $this->getMockBuilder(AdapterInterface::class) ->disableOriginalConstructor() ->getMock(); - $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock); + + $this->productRelationMock = $this->getMockBuilder(Relation::class) + ->disableOriginalConstructor() + ->getMock(); $this->relationsDataSaver = $helper->getObject( RelationsDataSaver::class, [ - 'resource' => $this->resourceMock + 'resource' => $this->resourceMock, + 'productRelation' => $this->productRelationMock ] ); } @@ -53,7 +63,7 @@ public function testSaveOptions() { $options = [1, 2]; $table_name= 'catalog_product_bundle_option'; - + $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock); $this->resourceMock->expects($this->once()) ->method('getTableName') ->with('catalog_product_bundle_option') @@ -78,6 +88,7 @@ public function testSaveOptionValues() $optionsValues = [1, 2]; $table_name= 'catalog_product_bundle_option_value'; + $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock); $this->resourceMock->expects($this->once()) ->method('getTableName') ->with('catalog_product_bundle_option_value') @@ -98,6 +109,7 @@ public function testSaveSelections() $selections = [1, 2]; $table_name= 'catalog_product_bundle_selection'; + $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->connectionMock); $this->resourceMock->expects($this->once()) ->method('getTableName') ->with('catalog_product_bundle_selection') @@ -121,4 +133,16 @@ public function testSaveSelections() $this->relationsDataSaver->saveSelections($selections); } + + public function testSaveProductRelations() + { + $parentId = 1; + $children = [2, 3]; + + $this->productRelationMock->expects($this->once()) + ->method('processRelations') + ->with($parentId, $children); + + $this->relationsDataSaver->saveProductRelations($parentId, $children); + } }