88use Magento \Bundle \Api \Data \OptionInterfaceFactory as OptionFactory ;
99use Magento \Bundle \Api \Data \LinkInterfaceFactory as LinkFactory ;
1010use Magento \Catalog \Api \Data \ProductCustomOptionInterfaceFactory ;
11+ use Magento \Catalog \Api \Data \ProductInterface ;
1112use Magento \Catalog \Api \ProductRepositoryInterface as ProductRepository ;
1213use Magento \Store \Model \StoreManagerInterface as StoreManager ;
1314use Magento \Framework \App \RequestInterface ;
1415
1516/**
16- * Class Bundle
17+ * Plugin class to initialize Bundle product
18+ *
1719 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1820 */
1921class Bundle
@@ -106,12 +108,14 @@ public function afterInitialize(
106108 $ product ->setBundleOptionsData ($ result ['bundle_options ' ]);
107109 }
108110
111+ if (!$ result ['bundle_selections ' ]) {
112+ $ this ->resetBundleProductOptions ($ product );
113+ }
114+
109115 $ this ->processBundleOptionsData ($ product );
110116 $ this ->processDynamicOptionsData ($ product );
111117 } elseif (!$ compositeReadonly ) {
112- $ extension = $ product ->getExtensionAttributes ();
113- $ extension ->setBundleProductOptions ([]);
114- $ product ->setExtensionAttributes ($ extension );
118+ $ this ->resetBundleProductOptions ($ product );
115119 }
116120
117121 $ affectProductSelections = (bool )$ this ->request ->getPost ('affect_bundle_product_selections ' );
@@ -120,6 +124,8 @@ public function afterInitialize(
120124 }
121125
122126 /**
127+ * Process Bundle Options Data
128+ *
123129 * @param \Magento\Catalog\Model\Product $product
124130 * @return void
125131 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -161,10 +167,11 @@ protected function processBundleOptionsData(\Magento\Catalog\Model\Product $prod
161167 $ extension = $ product ->getExtensionAttributes ();
162168 $ extension ->setBundleProductOptions ($ options );
163169 $ product ->setExtensionAttributes ($ extension );
164- return ;
165170 }
166171
167172 /**
173+ * Process Dynamic Options Data
174+ *
168175 * @param \Magento\Catalog\Model\Product $product
169176 * @return void
170177 */
@@ -198,9 +205,10 @@ protected function processDynamicOptionsData(\Magento\Catalog\Model\Product $pro
198205 }
199206
200207 /**
208+ * Build product link
209+ *
201210 * @param \Magento\Catalog\Model\Product $product
202211 * @param array $linkData
203- *
204212 * @return \Magento\Bundle\Api\Data\LinkInterface
205213 */
206214 private function buildLink (
@@ -228,4 +236,18 @@ private function buildLink(
228236
229237 return $ link ;
230238 }
239+
240+ /**
241+ * Resets bundle product options inside product extension attributes
242+ *
243+ * @param ProductInterface $product
244+ * @return void
245+ */
246+ private function resetBundleProductOptions (ProductInterface $ product ) : void
247+ {
248+ $ extension = $ product ->getExtensionAttributes ();
249+ $ extension ->setBundleProductOptions ([]);
250+ $ product ->setExtensionAttributes ($ extension );
251+ $ product ->setDropOptions (true );
252+ }
231253}
0 commit comments