Skip to content

Commit ec8830f

Browse files
committed
MAGETWO-90999: Bundle Product not appears in catalog after import
- Added bundle product entity relations to import
1 parent f38cc97 commit ec8830f

File tree

2 files changed

+63
-13
lines changed
  • app/code/Magento/BundleImportExport/Model/Import/Product/Type
  • dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type

2 files changed

+63
-13
lines changed

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

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
*/
99
namespace Magento\BundleImportExport\Model\Import\Product\Type;
1010

11-
use \Magento\Framework\App\ObjectManager;
12-
use \Magento\Bundle\Model\Product\Price as BundlePrice;
13-
use \Magento\Catalog\Model\Product\Type\AbstractType;
14-
use \Magento\CatalogImportExport\Model\Import\Product;
15-
use \Magento\Store\Model\StoreManagerInterface;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Bundle\Model\Product\Price as BundlePrice;
13+
use Magento\Catalog\Model\Product\Type\AbstractType;
14+
use Magento\CatalogImportExport\Model\Import\Product;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Bundle\Model\ResourceModel\Bundle as BundleResourceModel;
1617

1718
/**
1819
* Class Bundle
@@ -148,6 +149,11 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
148149
*/
149150
private $storeCodeToId = [];
150151

152+
/**
153+
* @var BundleResourceModel
154+
*/
155+
private $bundleResourceModel;
156+
151157
/**
152158
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac
153159
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac
@@ -156,8 +162,8 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
156162
* @param \Magento\Framework\EntityManager\MetadataPool|null $metadataPool
157163
* @param Bundle\RelationsDataSaver|null $relationsDataSaver
158164
* @param StoreManagerInterface $storeManager
165+
* @param BundleResourceModel $bundleResourceModel
159166
* @throws \Magento\Framework\Exception\LocalizedException
160-
* @throws \RuntimeException
161167
*/
162168
public function __construct(
163169
\Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac,
@@ -166,13 +172,16 @@ public function __construct(
166172
array $params,
167173
\Magento\Framework\EntityManager\MetadataPool $metadataPool = null,
168174
Bundle\RelationsDataSaver $relationsDataSaver = null,
169-
StoreManagerInterface $storeManager = null
175+
StoreManagerInterface $storeManager = null,
176+
BundleResourceModel $bundleResourceModel = null
170177
) {
171178
parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params, $metadataPool);
172179
$this->relationsDataSaver = $relationsDataSaver
173180
?: ObjectManager::getInstance()->get(Bundle\RelationsDataSaver::class);
174181
$this->storeManager = $storeManager
175182
?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
183+
$this->bundleResourceModel = $bundleResourceModel
184+
?: ObjectManager::getInstance()->get(BundleResourceModel::class);
176185
}
177186

178187
/**
@@ -389,13 +398,15 @@ public function saveData()
389398
if ($this->_type != $productData['type_id']) {
390399
continue;
391400
}
392-
$this->parseSelections($rowData, $productData[$this->getProductEntityLinkField()]);
401+
$productId = $productData[$this->getProductEntityLinkField()];
402+
$this->parseSelections($rowData, $productId);
393403
}
394404
if (!empty($this->_cachedOptions)) {
395405
$this->retrieveProducsByCachedSkus();
396406
$this->populateExistingOptions();
397407
$this->insertOptions();
398408
$this->insertSelections();
409+
$this->insertProductRelations();
399410
$this->clear();
400411
}
401412
}
@@ -643,6 +654,29 @@ protected function insertSelections()
643654
return $this;
644655
}
645656

657+
/**
658+
* Insert product relations.
659+
*
660+
* @return void
661+
*/
662+
private function insertProductRelations()
663+
{
664+
foreach ($this->_cachedOptions as $productId => $options) {
665+
$childIds = [];
666+
foreach ($options as $option) {
667+
foreach ($option['selections'] as $selection) {
668+
if (isset($this->_cachedSkuToProducts[$selection['sku']])) {
669+
$childIds[] = $this->_cachedSkuToProducts[$selection['sku']];
670+
}
671+
}
672+
}
673+
if (!empty($childIds)) {
674+
$childIds = array_unique($childIds);
675+
$this->bundleResourceModel->saveProductRelations($productId, $childIds);
676+
}
677+
}
678+
}
679+
646680
/**
647681
* Initialize attributes parameters for all attributes' sets.
648682
*

dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type/BundleTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
*/
66
namespace Magento\BundleImportExport\Model\Import\Product\Type;
77

8-
use Magento\Framework\App\Bootstrap;
98
use Magento\Framework\App\Filesystem\DirectoryList;
10-
use Magento\ImportExport\Model\Import;
9+
use Magento\Framework\Indexer\IndexerRegistry;
1110

1211
/**
1312
* @magentoAppArea adminhtml
13+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1414
*/
1515
class BundleTest extends \PHPUnit\Framework\TestCase
1616
{
@@ -49,7 +49,7 @@ protected function setUp()
4949

5050
/**
5151
* @magentoAppArea adminhtml
52-
* @magentoDbIsolation enabled
52+
* @magentoDbIsolation disabled
5353
* @magentoAppIsolation enabled
5454
*/
5555
public function testBundleImport()
@@ -80,12 +80,17 @@ public function testBundleImport()
8080
$this->assertTrue($errors->getErrorsCount() == 0);
8181
$this->model->importData();
8282

83+
$indexerRegistry = $this->objectManager->get(IndexerRegistry::class);
84+
$indexerRegistry->get('cataloginventory_stock')->reindexAll();
85+
86+
/** @var \Magento\Catalog\Model\ResourceModel\Product $resource */
8387
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
8488
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
8589
$this->assertTrue(is_numeric($productId));
90+
8691
/** @var \Magento\Catalog\Model\Product $product */
87-
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
88-
$product->load($productId);
92+
$productRepository = $this->objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
93+
$product = $productRepository->get(self::TEST_PRODUCT_NAME, false, null, true);
8994

9095
$this->assertFalse($product->isObjectNew());
9196
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
@@ -106,6 +111,17 @@ public function testBundleImport()
106111
$this->assertEquals($optionSku, $productLink->getData('sku'));
107112
}
108113
}
114+
115+
/** @var \Magento\CatalogInventory\Model\Stock\Item $stockItem */
116+
$stockItem = $product->getExtensionAttributes()->getStockItem();
117+
$this->assertTrue(
118+
$stockItem->getIsInStock(),
119+
'Imported bundle product should be in stock'
120+
);
121+
$this->assertTrue(
122+
$product->isSalable(),
123+
'Imported bundle product should be available for sale'
124+
);
109125
}
110126

111127
/**

0 commit comments

Comments
 (0)