@@ -87,71 +87,115 @@ public function afterInitialize(
87
87
\Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper $ subject ,
88
88
\Magento \Catalog \Model \Product $ product
89
89
) {
90
- if (($ selections = $ this ->request ->getPost ('bundle_selections ' )) && !$ product ->getCompositeReadonly ()) {
90
+ $ compositeReadonly = $ product ->getCompositeReadonly ();
91
+ $ selections = $ this ->request ->getPost ('bundle_selections ' );
92
+ if ($ selections && !$ compositeReadonly ) {
91
93
$ product ->setBundleSelectionsData ($ selections );
92
94
}
93
- if (($ items = $ this ->request ->getPost ('bundle_options ' )) && !$ product ->getCompositeReadonly ()) {
95
+
96
+ $ items = $ this ->request ->getPost ('bundle_options ' );
97
+ if ($ items && !$ compositeReadonly ) {
94
98
$ product ->setBundleOptionsData ($ items );
95
99
}
96
100
97
- if ($ product ->getBundleOptionsData ()) {
98
- $ options = [];
99
- foreach ($ product ->getBundleOptionsData () as $ key => $ optionData ) {
100
- if (!(bool )$ optionData ['delete ' ]) {
101
- $ option = $ this ->optionFactory ->create (['data ' => $ optionData ]);
102
- $ option ->setSku ($ product ->getSku ());
103
- $ option ->setOptionId (null );
104
-
105
- $ links = [];
106
- $ bundleLinks = $ product ->getBundleSelectionsData ();
107
- if (!empty ($ bundleLinks [$ key ])) {
108
- foreach ($ bundleLinks [$ key ] as $ linkData ) {
109
- if (!(bool )$ linkData ['delete ' ]) {
110
- $ link = $ this ->linkFactory ->create (['data ' => $ linkData ]);
111
- $ linkProduct = $ this ->productRepository ->getById ($ linkData ['product_id ' ]);
112
- $ link ->setSku ($ linkProduct ->getSku ());
113
- $ link ->setQty ($ linkData ['selection_qty ' ]);
114
- if (isset ($ linkData ['selection_can_change_qty ' ])) {
115
- $ link ->setCanChangeQuantity ($ linkData ['selection_can_change_qty ' ]);
116
- }
117
- $ links [] = $ link ;
118
- }
119
- }
120
- $ option ->setProductLinks ($ links );
121
- $ options [] = $ option ;
122
- }
123
- }
124
- }
125
- $ extension = $ product ->getExtensionAttributes ();
126
- $ extension ->setBundleProductOptions ($ options );
127
- $ product ->setExtensionAttributes ($ extension );
101
+ $ this ->processBundleOptionsData ($ product );
102
+
103
+ $ this ->processDynamicOptionsData ($ product );
104
+
105
+ $ affectProductSelections = (bool )$ this ->request ->getPost ('affect_bundle_product_selections ' );
106
+ $ product ->setCanSaveBundleSelections ($ affectProductSelections && !$ compositeReadonly );
107
+ return $ product ;
108
+ }
109
+
110
+ /**
111
+ * @param \Magento\Catalog\Model\Product $product
112
+ * @return void
113
+ */
114
+ protected function processBundleOptionsData (\Magento \Catalog \Model \Product $ product )
115
+ {
116
+ $ bundleOptionsData = $ product ->getBundleOptionsData ();
117
+ if (!$ bundleOptionsData ) {
118
+ return ;
128
119
}
120
+ $ options = [];
121
+ foreach ($ bundleOptionsData as $ key => $ optionData ) {
122
+ if ((bool )$ optionData ['delete ' ]) {
123
+ continue ;
124
+ }
125
+ $ option = $ this ->optionFactory ->create (['data ' => $ optionData ]);
126
+ $ option ->setSku ($ product ->getSku ());
127
+ $ option ->setOptionId (null );
128
+
129
+ $ links = [];
130
+ $ bundleLinks = $ product ->getBundleSelectionsData ();
131
+ if (empty ($ bundleLinks [$ key ])) {
132
+ continue ;
133
+ }
134
+
135
+ foreach ($ bundleLinks [$ key ] as $ linkData ) {
136
+ if ((bool )$ linkData ['delete ' ]) {
137
+ continue ;
138
+ }
139
+ $ link = $ this ->linkFactory ->create (['data ' => $ linkData ]);
129
140
130
- if (
131
- $ product ->getPriceType () === \Magento \Bundle \Model \Product \Price::PRICE_TYPE_DYNAMIC
132
- && !$ product ->getOptionsReadonly ()
133
- ) {
134
- $ product ->setCanSaveCustomOptions (true );
135
- if ($ customOptions = $ product ->getProductOptions ()) {
136
- foreach (array_keys ($ customOptions ) as $ key ) {
137
- $ customOptions [$ key ]['is_delete ' ] = 1 ;
141
+ if (array_key_exists ('selection_price_value ' , $ linkData )) {
142
+ $ link ->setPrice ($ linkData ['selection_price_value ' ]);
138
143
}
139
- $ newOptions = $ product ->getOptions ();
140
- foreach ($ customOptions as $ customOptionData ) {
141
- if (!(bool )$ customOptionData ['is_delete ' ]) {
142
- $ customOption = $ this ->customOptionFactory ->create (['data ' => $ customOptionData ]);
143
- $ customOption ->setProductSku ($ product ->getSku ());
144
- $ customOption ->setOptionId (null );
145
- $ newOptions [] = $ customOption ;
146
- }
144
+
145
+ if (array_key_exists ('selection_price_type ' , $ linkData )) {
146
+ $ link ->setPriceType ($ linkData ['selection_price_type ' ]);
147
147
}
148
- $ product ->setOptions ($ newOptions );
148
+
149
+ $ linkProduct = $ this ->productRepository ->getById ($ linkData ['product_id ' ]);
150
+ $ link ->setSku ($ linkProduct ->getSku ());
151
+ $ link ->setQty ($ linkData ['selection_qty ' ]);
152
+
153
+ if (array_key_exists ('selection_can_change_qty ' , $ linkData )) {
154
+ $ link ->setCanChangeQuantity ($ linkData ['selection_can_change_qty ' ]);
155
+ }
156
+ $ links [] = $ link ;
149
157
}
158
+ $ option ->setProductLinks ($ links );
159
+ $ options [] = $ option ;
150
160
}
151
161
152
- $ product ->setCanSaveBundleSelections (
153
- (bool )$ this ->request ->getPost ('affect_bundle_product_selections ' ) && !$ product ->getCompositeReadonly ()
154
- );
155
- return $ product ;
162
+ $ extension = $ product ->getExtensionAttributes ();
163
+ $ extension ->setBundleProductOptions ($ options );
164
+ $ product ->setExtensionAttributes ($ extension );
165
+ return ;
166
+ }
167
+
168
+ /**
169
+ * @param \Magento\Catalog\Model\Product $product
170
+ * @return void
171
+ */
172
+ protected function processDynamicOptionsData (\Magento \Catalog \Model \Product $ product )
173
+ {
174
+ if ($ product ->getPriceType () !== \Magento \Bundle \Model \Product \Price::PRICE_TYPE_DYNAMIC ) {
175
+ return ;
176
+ }
177
+
178
+ if ($ product ->getOptionsReadonly ()) {
179
+ return ;
180
+ }
181
+ $ product ->setCanSaveCustomOptions (true );
182
+ $ customOptions = $ product ->getProductOptions ();
183
+ if (!$ customOptions ) {
184
+ return ;
185
+ }
186
+ foreach (array_keys ($ customOptions ) as $ key ) {
187
+ $ customOptions [$ key ]['is_delete ' ] = 1 ;
188
+ }
189
+ $ newOptions = $ product ->getOptions ();
190
+ foreach ($ customOptions as $ customOptionData ) {
191
+ if ((bool )$ customOptionData ['is_delete ' ]) {
192
+ continue ;
193
+ }
194
+ $ customOption = $ this ->customOptionFactory ->create (['data ' => $ customOptionData ]);
195
+ $ customOption ->setProductSku ($ product ->getSku ());
196
+ $ customOption ->setOptionId (null );
197
+ $ newOptions [] = $ customOption ;
198
+ }
199
+ $ product ->setOptions ($ newOptions );
156
200
}
157
201
}
0 commit comments