6
6
namespace Magento \BundleSampleData \Model ;
7
7
8
8
use Magento \Framework \Setup \SampleData \Context as SampleDataContext ;
9
+ use Magento \Bundle \Api \Data \OptionInterfaceFactory as OptionFactory ;
10
+ use Magento \Bundle \Api \Data \LinkInterfaceFactory as LinkFactory ;
11
+ use Magento \Catalog \Api \ProductRepositoryInterface as ProductRepository ;
9
12
10
13
/**
11
14
* Setup bundle product
@@ -17,6 +20,21 @@ class Product extends \Magento\CatalogSampleData\Model\Product
17
20
*/
18
21
protected $ productType = \Magento \Catalog \Model \Product \Type::TYPE_BUNDLE ;
19
22
23
+ /**
24
+ * @var OptionFactory
25
+ */
26
+ protected $ optionFactory ;
27
+
28
+ /**
29
+ * @var LinkFactory
30
+ */
31
+ protected $ linkFactory ;
32
+
33
+ /**
34
+ * @var ProductRepository
35
+ */
36
+ protected $ productRepository ;
37
+
20
38
/**
21
39
* Product constructor.
22
40
* @param SampleDataContext $sampleDataContext
@@ -34,7 +52,10 @@ public function __construct(
34
52
\Magento \BundleSampleData \Model \Product \Converter $ converter ,
35
53
\Magento \CatalogSampleData \Model \Product \Gallery $ gallery ,
36
54
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
37
- \Magento \Eav \Model \Config $ eavConfig
55
+ \Magento \Eav \Model \Config $ eavConfig ,
56
+ OptionFactory $ optionFactory ,
57
+ LinkFactory $ linkFactory ,
58
+ ProductRepository $ productRepository
38
59
) {
39
60
$ this ->eavConfig = $ eavConfig ;
40
61
parent ::__construct (
@@ -46,6 +67,9 @@ public function __construct(
46
67
$ storeManager ,
47
68
$ eavConfig
48
69
);
70
+ $ this ->optionFactory = $ optionFactory ;
71
+ $ this ->linkFactory = $ linkFactory ;
72
+ $ this ->productRepository = $ productRepository ;
49
73
}
50
74
51
75
/**
@@ -57,6 +81,34 @@ protected function prepareProduct($product, $data)
57
81
->setCanSaveConfigurableAttributes (true )
58
82
->setCanSaveBundleSelections (true )
59
83
->setPriceType (0 );
84
+ $ bundleOptionsData = $ product ->getBundleOptionsData ();
85
+ $ options = [];
86
+ foreach ($ bundleOptionsData as $ key => $ optionData ) {
87
+
88
+ $ option = $ this ->optionFactory ->create (['data ' => $ optionData ]);
89
+ $ option ->setSku ($ product ->getSku ());
90
+ $ option ->setOptionId (null );
91
+
92
+ $ links = [];
93
+ $ bundleLinks = $ product ->getBundleSelectionsData ();
94
+ foreach ($ bundleLinks [$ key ] as $ linkData ) {
95
+ $ linkProduct = $ this ->productRepository ->getById ($ linkData ['product_id ' ]);
96
+ $ link = $ this ->linkFactory ->create (['data ' => $ linkData ]);
97
+ $ link ->setSku ($ linkProduct ->getSku ());
98
+ $ link ->setQty ($ linkData ['selection_qty ' ]);
99
+
100
+ if (array_key_exists ('selection_can_change_qty ' , $ linkData )) {
101
+ $ link ->setCanChangeQuantity ($ linkData ['selection_can_change_qty ' ]);
102
+ }
103
+ $ links [] = $ link ;
104
+ }
105
+ $ option ->setProductLinks ($ links );
106
+ $ options [] = $ option ;
107
+ }
108
+
109
+ $ extension = $ product ->getExtensionAttributes ();
110
+ $ extension ->setBundleProductOptions ($ options );
111
+ $ product ->setExtensionAttributes ($ extension );
60
112
61
113
return $ this ;
62
114
}
0 commit comments