19
19
use Magento \Catalog \Api \ProductRepositoryInterface ;
20
20
use Magento \Catalog \Model \Product ;
21
21
use Magento \Framework \Api \DataObjectHelper ;
22
+ use Magento \Framework \EntityManager \MetadataPool ;
22
23
use Magento \Framework \Exception \CouldNotSaveException ;
23
24
use Magento \Framework \Exception \InputException ;
24
- use Magento \Framework \EntityManager \MetadataPool ;
25
25
use Magento \Framework \Exception \NoSuchEntityException ;
26
26
use Magento \Store \Model \StoreManagerInterface ;
27
27
@@ -174,11 +174,12 @@ public function saveChild(
174
174
);
175
175
}
176
176
$ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
177
- $ selectionModel = $ this ->mapProductLinkToSelectionModel (
177
+ $ selectionModel = $ this ->mapProductLinkToBundleSelectionModel (
178
178
$ selectionModel ,
179
179
$ linkedProduct ,
180
- $ linkProductModel ->getId (),
181
- $ product ->getData ($ linkField )
180
+ $ product ,
181
+ (int )$ linkProductModel ->getId (),
182
+ $ linkField
182
183
);
183
184
184
185
try {
@@ -202,6 +203,7 @@ public function saveChild(
202
203
*
203
204
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
204
205
* @SuppressWarnings(PHPMD.NPathComplexity)
206
+ * @deprecated use mapProductLinkToBundleSelectionModel
205
207
*/
206
208
protected function mapProductLinkToSelectionModel (
207
209
Selection $ selectionModel ,
@@ -235,9 +237,55 @@ protected function mapProductLinkToSelectionModel(
235
237
if ($ productLink ->getIsDefault () !== null ) {
236
238
$ selectionModel ->setIsDefault ($ productLink ->getIsDefault ());
237
239
}
238
- if ($ productLink ->getWebsiteId () !== null ) {
239
- $ selectionModel ->setWebsiteId ($ productLink ->getWebsiteId ());
240
+
241
+ return $ selectionModel ;
242
+ }
243
+
244
+ /**
245
+ * Fill selection model with product link data.
246
+ *
247
+ * @param Selection $selectionModel
248
+ * @param LinkInterface $productLink
249
+ * @param ProductInterface $parentProduct
250
+ * @param int $linkedProductId
251
+ * @param string $linkField
252
+ * @return Selection
253
+ * @throws NoSuchEntityException
254
+ */
255
+ private function mapProductLinkToBundleSelectionModel (
256
+ Selection $ selectionModel ,
257
+ LinkInterface $ productLink ,
258
+ ProductInterface $ parentProduct ,
259
+ int $ linkedProductId ,
260
+ string $ linkField
261
+ ): Selection {
262
+ $ selectionModel ->setProductId ($ linkedProductId );
263
+ $ selectionModel ->setParentProductId ($ parentProduct ->getData ($ linkField ));
264
+ if ($ productLink ->getSelectionId () !== null ) {
265
+ $ selectionModel ->setSelectionId ($ productLink ->getSelectionId ());
266
+ }
267
+ if ($ productLink ->getOptionId () !== null ) {
268
+ $ selectionModel ->setOptionId ($ productLink ->getOptionId ());
240
269
}
270
+ if ($ productLink ->getPosition () !== null ) {
271
+ $ selectionModel ->setPosition ($ productLink ->getPosition ());
272
+ }
273
+ if ($ productLink ->getQty () !== null ) {
274
+ $ selectionModel ->setSelectionQty ($ productLink ->getQty ());
275
+ }
276
+ if ($ productLink ->getPriceType () !== null ) {
277
+ $ selectionModel ->setSelectionPriceType ($ productLink ->getPriceType ());
278
+ }
279
+ if ($ productLink ->getPrice () !== null ) {
280
+ $ selectionModel ->setSelectionPriceValue ($ productLink ->getPrice ());
281
+ }
282
+ if ($ productLink ->getCanChangeQuantity () !== null ) {
283
+ $ selectionModel ->setSelectionCanChangeQty ($ productLink ->getCanChangeQuantity ());
284
+ }
285
+ if ($ productLink ->getIsDefault () !== null ) {
286
+ $ selectionModel ->setIsDefault ($ productLink ->getIsDefault ());
287
+ }
288
+ $ selectionModel ->setWebsiteId ((int )$ this ->storeManager ->getStore ($ parentProduct ->getStoreId ())->getWebsiteId ());
241
289
242
290
return $ selectionModel ;
243
291
}
@@ -305,12 +353,14 @@ public function addChild(
305
353
}
306
354
307
355
$ selectionModel = $ this ->bundleSelection ->create ();
308
- $ selectionModel = $ this ->mapProductLinkToSelectionModel (
356
+ $ selectionModel = $ this ->mapProductLinkToBundleSelectionModel (
309
357
$ selectionModel ,
310
358
$ linkedProduct ,
311
- $ linkProductModel ->getEntityId (),
312
- $ product ->getData ($ linkField )
359
+ $ product ,
360
+ (int )$ linkProductModel ->getEntityId (),
361
+ $ linkField
313
362
);
363
+
314
364
$ selectionModel ->setOptionId ($ optionId );
315
365
316
366
try {
0 commit comments