8
8
*/
9
9
namespace Magento \BundleImportExport \Model \Import \Product \Type ;
10
10
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 ;
16
17
17
18
/**
18
19
* Class Bundle
@@ -148,6 +149,11 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
148
149
*/
149
150
private $ storeCodeToId = [];
150
151
152
+ /**
153
+ * @var BundleResourceModel
154
+ */
155
+ private $ bundleResourceModel ;
156
+
151
157
/**
152
158
* @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac
153
159
* @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac
@@ -156,8 +162,8 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
156
162
* @param \Magento\Framework\EntityManager\MetadataPool|null $metadataPool
157
163
* @param Bundle\RelationsDataSaver|null $relationsDataSaver
158
164
* @param StoreManagerInterface $storeManager
165
+ * @param BundleResourceModel $bundleResourceModel
159
166
* @throws \Magento\Framework\Exception\LocalizedException
160
- * @throws \RuntimeException
161
167
*/
162
168
public function __construct (
163
169
\Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \CollectionFactory $ attrSetColFac ,
@@ -166,13 +172,16 @@ public function __construct(
166
172
array $ params ,
167
173
\Magento \Framework \EntityManager \MetadataPool $ metadataPool = null ,
168
174
Bundle \RelationsDataSaver $ relationsDataSaver = null ,
169
- StoreManagerInterface $ storeManager = null
175
+ StoreManagerInterface $ storeManager = null ,
176
+ BundleResourceModel $ bundleResourceModel = null
170
177
) {
171
178
parent ::__construct ($ attrSetColFac , $ prodAttrColFac , $ resource , $ params , $ metadataPool );
172
179
$ this ->relationsDataSaver = $ relationsDataSaver
173
180
?: ObjectManager::getInstance ()->get (Bundle \RelationsDataSaver::class);
174
181
$ this ->storeManager = $ storeManager
175
182
?: ObjectManager::getInstance ()->get (StoreManagerInterface::class);
183
+ $ this ->bundleResourceModel = $ bundleResourceModel
184
+ ?: ObjectManager::getInstance ()->get (BundleResourceModel::class);
176
185
}
177
186
178
187
/**
@@ -389,13 +398,15 @@ public function saveData()
389
398
if ($ this ->_type != $ productData ['type_id ' ]) {
390
399
continue ;
391
400
}
392
- $ this ->parseSelections ($ rowData , $ productData [$ this ->getProductEntityLinkField ()]);
401
+ $ productId = $ productData [$ this ->getProductEntityLinkField ()];
402
+ $ this ->parseSelections ($ rowData , $ productId );
393
403
}
394
404
if (!empty ($ this ->_cachedOptions )) {
395
405
$ this ->retrieveProducsByCachedSkus ();
396
406
$ this ->populateExistingOptions ();
397
407
$ this ->insertOptions ();
398
408
$ this ->insertSelections ();
409
+ $ this ->insertProductRelations ();
399
410
$ this ->clear ();
400
411
}
401
412
}
@@ -643,6 +654,29 @@ protected function insertSelections()
643
654
return $ this ;
644
655
}
645
656
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
+
646
680
/**
647
681
* Initialize attributes parameters for all attributes' sets.
648
682
*
0 commit comments