@@ -75,15 +75,18 @@ public function __construct(
75
75
/**
76
76
* {@inheritdoc}
77
77
*/
78
- public function getChildren ($ productId )
78
+ public function getChildren ($ productSku , $ optionId = null )
79
79
{
80
- $ product = $ this ->productRepository ->get ($ productId );
80
+ $ product = $ this ->productRepository ->get ($ productSku );
81
81
if ($ product ->getTypeId () != \Magento \Catalog \Model \Product \Type::TYPE_BUNDLE ) {
82
82
throw new InputException (__ ('Only implemented for bundle product ' ));
83
83
}
84
84
85
85
$ childrenList = [];
86
86
foreach ($ this ->getOptions ($ product ) as $ option ) {
87
+ if ($ optionId !== null && $ option ->getOptionId () != $ optionId ) {
88
+ continue ;
89
+ }
87
90
/** @var \Magento\Catalog\Model\Product $selection */
88
91
foreach ($ option ->getSelections () as $ selection ) {
89
92
$ childrenList [] = $ this ->buildLink ($ selection , $ product );
@@ -107,6 +110,93 @@ public function addChildByProductSku($sku, $optionId, \Magento\Bundle\Api\Data\L
107
110
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
108
111
* @SuppressWarnings(PHPMD.NPathComplexity)
109
112
*/
113
+ public function saveChild (
114
+ $ sku ,
115
+ \Magento \Bundle \Api \Data \LinkInterface $ linkedProduct
116
+ ) {
117
+ $ product = $ this ->productRepository ->get ($ sku );
118
+ if ($ product ->getTypeId () != \Magento \Catalog \Model \Product \Type::TYPE_BUNDLE ) {
119
+ throw new InputException (
120
+ __ ('Product with specified sku: "%1" is not a bundle product ' , [$ product ->getSku ()])
121
+ );
122
+ }
123
+
124
+ /** @var \Magento\Catalog\Model\Product $linkProductModel */
125
+ $ linkProductModel = $ this ->productRepository ->get ($ linkedProduct ->getSku ());
126
+ if ($ linkProductModel ->isComposite ()) {
127
+ throw new InputException (__ ('Bundle product could not contain another composite product ' ));
128
+ }
129
+
130
+ if (!$ linkedProduct ->getId ()) {
131
+ throw new InputException (__ ('Id field of product link is required ' ));
132
+ }
133
+
134
+ /** @var \Magento\Bundle\Model\Selection $selectionModel */
135
+ $ selectionModel = $ this ->bundleSelection ->create ();
136
+ $ selectionModel ->load ($ linkedProduct ->getId ());
137
+ if (!$ selectionModel ->getId ()) {
138
+ throw new InputException (__ ('Can not find product link with id "%1" ' , [$ linkedProduct ->getId ()]));
139
+ }
140
+
141
+ $ selectionModel = $ this ->mapProductLinkToSelectionModel (
142
+ $ selectionModel ,
143
+ $ linkedProduct ,
144
+ $ linkProductModel ->getId (),
145
+ $ product ->getId ()
146
+ );
147
+
148
+ try {
149
+ $ selectionModel ->save ();
150
+ } catch (\Exception $ e ) {
151
+ throw new CouldNotSaveException (__ ('Could not save child: "%1" ' , $ e ->getMessage ()), $ e );
152
+ }
153
+
154
+ return true ;
155
+ }
156
+
157
+ /**
158
+ * @param \Magento\Bundle\Model\Selection $selectionModel
159
+ * @param \Magento\Bundle\Api\Data\LinkInterface $productLink
160
+ * @param string $linkedProductId
161
+ * @param string $parentProductId
162
+ * @return \Magento\Bundle\Model\Selection
163
+ */
164
+ protected function mapProductLinkToSelectionModel (
165
+ \Magento \Bundle \Model \Selection $ selectionModel ,
166
+ \Magento \Bundle \Api \Data \LinkInterface $ productLink ,
167
+ $ linkedProductId ,
168
+ $ parentProductId
169
+ ) {
170
+ $ selectionModel ->setProductId ($ linkedProductId );
171
+ $ selectionModel ->setParentProductId ($ parentProductId );
172
+ if (($ productLink ->getOptionId () !== null )) {
173
+ $ selectionModel ->setOptionId ($ productLink ->getOptionId ());
174
+ }
175
+ if ($ productLink ->getPosition () !== null ) {
176
+ $ selectionModel ->setPosition ($ productLink ->getPosition ());
177
+ }
178
+ if ($ productLink ->getQty () !== null ) {
179
+ $ selectionModel ->setSelectionQty ($ productLink ->getQty ());
180
+ }
181
+ if ($ productLink ->getPriceType () !== null ) {
182
+ $ selectionModel ->setSelectionPriceType ($ productLink ->getPriceType ());
183
+ }
184
+ if ($ productLink ->getPrice () !== null ) {
185
+ $ selectionModel ->setSelectionPriceValue ($ productLink ->getPrice ());
186
+ }
187
+ if ($ productLink ->getCanChangeQuantity () !== null ) {
188
+ $ selectionModel ->setSelectionCanChangeQty ($ productLink ->getCanChangeQuantity ());
189
+ }
190
+ if ($ productLink ->getIsDefault () !== null ) {
191
+ $ selectionModel ->setIsDefault ($ productLink ->getIsDefault ());
192
+ }
193
+
194
+ return $ selectionModel ;
195
+ }
196
+
197
+ /**
198
+ * {@inheritdoc}
199
+ */
110
200
public function addChild (
111
201
\Magento \Catalog \Api \Data \ProductInterface $ product ,
112
202
$ optionId ,
@@ -119,17 +209,10 @@ public function addChild(
119
209
}
120
210
121
211
$ options = $ this ->optionCollection ->create ();
122
- $ options ->setProductIdFilter ($ product ->getId ())->joinValues ($ this ->storeManager ->getStore ()->getId ());
123
- $ isNewOption = true ;
124
- /** @var \Magento\Bundle\Model\Option $option */
125
- foreach ($ options as $ option ) {
126
- if ($ option ->getOptionId () == $ optionId ) {
127
- $ isNewOption = false ;
128
- break ;
129
- }
130
- }
212
+ $ options ->setIdFilter ($ optionId );
213
+ $ existingOption = $ options ->getFirstItem ();
131
214
132
- if ($ isNewOption ) {
215
+ if (! $ existingOption -> getId () ) {
133
216
throw new InputException (
134
217
__ (
135
218
'Product with specified sku: "%1" does not contain option: "%2" ' ,
@@ -161,16 +244,13 @@ public function addChild(
161
244
}
162
245
163
246
$ selectionModel = $ this ->bundleSelection ->create ();
164
- $ selectionModel ->setOptionId ($ optionId )
165
- ->setPosition ($ linkedProduct ->getPosition ())
166
- ->setSelectionQty ($ linkedProduct ->getQty ())
167
- ->setSelectionPriceType ($ linkedProduct ->getPriceType ())
168
- ->setSelectionPriceValue ($ linkedProduct ->getPrice ())
169
- ->setSelectionCanChangeQty ($ linkedProduct ->getCanChangeQuantity ())
170
- ->setProductId ($ linkProductModel ->getId ())
171
- ->setParentProductId ($ product ->getId ())
172
- ->setIsDefault ($ linkedProduct ->getIsDefault ())
173
- ->setWebsiteId ($ this ->storeManager ->getStore ()->getWebsiteId ());
247
+ $ selectionModel = $ this ->mapProductLinkToSelectionModel (
248
+ $ selectionModel ,
249
+ $ linkedProduct ,
250
+ $ linkProductModel ->getId (),
251
+ $ product ->getId ()
252
+ );
253
+ $ selectionModel ->setOptionId ($ optionId );
174
254
175
255
try {
176
256
$ selectionModel ->save ();
@@ -242,16 +322,17 @@ private function buildLink(\Magento\Catalog\Model\Product $selection, \Magento\C
242
322
'\Magento\Bundle\Api\Data\LinkInterface '
243
323
);
244
324
$ link ->setIsDefault ($ selection ->getIsDefault ())
325
+ ->setId ($ selection ->getSelectionId ())
245
326
->setQty ($ selection ->getSelectionQty ())
246
- ->setIsDefined ($ selection ->getSelectionCanChangeQty ())
327
+ ->setCanChangeQuantity ($ selection ->getSelectionCanChangeQty ())
247
328
->setPrice ($ selectionPrice )
248
329
->setPriceType ($ selectionPriceType );
249
330
return $ link ;
250
331
}
251
332
252
333
/**
253
334
* @param \Magento\Catalog\Api\Data\ProductInterface $product
254
- * @return \Magento\Bundle\Api\Data\OptionTypeInterface []
335
+ * @return \Magento\Bundle\Api\Data\OptionInterface []
255
336
*/
256
337
private function getOptions (\Magento \Catalog \Api \Data \ProductInterface $ product )
257
338
{
0 commit comments