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 ;
12
+ use \Magento \Framework \App \ObjectManager ;
9
13
10
14
/**
11
15
* Setup bundle product
@@ -17,6 +21,11 @@ class Product extends \Magento\CatalogSampleData\Model\Product
17
21
*/
18
22
protected $ productType = \Magento \Catalog \Model \Product \Type::TYPE_BUNDLE ;
19
23
24
+ /**
25
+ * @var OptionFactory
26
+ */
27
+ private $ optionFactory ;
28
+
20
29
/**
21
30
* Product constructor.
22
31
* @param SampleDataContext $sampleDataContext
@@ -48,6 +57,16 @@ public function __construct(
48
57
);
49
58
}
50
59
60
+ /**
61
+ * @var LinkFactory
62
+ */
63
+ private $ linkFactory ;
64
+
65
+ /**
66
+ * @var ProductRepository
67
+ */
68
+ private $ productRepository ;
69
+
51
70
/**
52
71
* @inheritdoc
53
72
*/
@@ -57,7 +76,88 @@ protected function prepareProduct($product, $data)
57
76
->setCanSaveConfigurableAttributes (true )
58
77
->setCanSaveBundleSelections (true )
59
78
->setPriceType (0 );
79
+ $ bundleOptionsData = $ product ->getBundleOptionsData ();
80
+ $ options = [];
81
+ foreach ($ bundleOptionsData as $ key => $ optionData ) {
82
+ $ option = $ this ->getOptionFactory ()->create (['data ' => $ optionData ]);
83
+ $ option ->setSku ($ product ->getSku ());
84
+ $ option ->setOptionId (null );
85
+
86
+ $ links = [];
87
+ $ bundleLinks = $ product ->getBundleSelectionsData ();
88
+ foreach ($ bundleLinks [$ key ] as $ linkData ) {
89
+ $ linkProduct = $ this ->getProductRepository ()->getById ($ linkData ['product_id ' ]);
90
+ $ link = $ this ->getLinkFactory ()->create (['data ' => $ linkData ]);
91
+ $ link ->setSku ($ linkProduct ->getSku ());
92
+ $ link ->setQty ($ linkData ['selection_qty ' ]);
93
+
94
+ if (array_key_exists ('selection_can_change_qty ' , $ linkData )) {
95
+ $ link ->setCanChangeQuantity ($ linkData ['selection_can_change_qty ' ]);
96
+ }
97
+ $ links [] = $ link ;
98
+ }
99
+ $ option ->setProductLinks ($ links );
100
+ $ options [] = $ option ;
101
+ }
102
+
103
+ $ extension = $ product ->getExtensionAttributes ();
104
+ $ extension ->setBundleProductOptions ($ options );
105
+ $ product ->setExtensionAttributes ($ extension );
60
106
61
107
return $ this ;
62
108
}
109
+
110
+ /**
111
+ * Get option interface factory
112
+ *
113
+ * @deprecated
114
+ * @return \Magento\Bundle\Api\Data\OptionInterfaceFactory
115
+ */
116
+ private function getOptionFactory ()
117
+ {
118
+
119
+ if (!($ this ->optionFactory )) {
120
+ return ObjectManager::getInstance ()->get (
121
+ '\Magento\Bundle\Api\Data\OptionInterfaceFactory '
122
+ );
123
+ } else {
124
+ return $ this ->optionFactory ;
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Get bundle link interface factory
130
+ *
131
+ * @deprecated
132
+ * @return \Magento\Bundle\Api\Data\LinkInterfaceFactory
133
+ */
134
+ private function getLinkFactory ()
135
+ {
136
+
137
+ if (!($ this ->linkFactory )) {
138
+ return ObjectManager::getInstance ()->get (
139
+ '\Magento\Bundle\Api\Data\LinkInterfaceFactory '
140
+ );
141
+ } else {
142
+ return $ this ->linkFactory ;
143
+ }
144
+ }
145
+
146
+ /**
147
+ * Get product repository
148
+ *
149
+ * @deprecated
150
+ * @return \Magento\Catalog\Api\ProductRepositoryInterface
151
+ */
152
+ private function getProductRepository ()
153
+ {
154
+
155
+ if (!($ this ->productRepository )) {
156
+ return ObjectManager::getInstance ()->get (
157
+ '\Magento\Catalog\Api\ProductRepositoryInterface '
158
+ );
159
+ } else {
160
+ return $ this ->productRepository ;
161
+ }
162
+ }
63
163
}
0 commit comments