88use Magento \Catalog \Api \Data \ProductCustomOptionInterfaceFactory as CustomOptionFactory ;
99use Magento \Catalog \Api \Data \ProductLinkInterfaceFactory as ProductLinkFactory ;
1010use Magento \Catalog \Api \ProductRepositoryInterface \Proxy as ProductRepository ;
11+ use Magento \Catalog \Model \Product ;
1112use Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks ;
1213use Magento \Catalog \Model \Product \Link \Resolver as LinkResolver ;
1314use Magento \Framework \App \ObjectManager ;
@@ -112,14 +113,14 @@ public function __construct(
112113 /**
113114 * Initialize product from data
114115 *
115- * @param \Magento\Catalog\Model\ Product $product
116+ * @param Product $product
116117 * @param array $productData
117- * @return \Magento\Catalog\Model\ Product
118+ * @return Product
118119 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
119120 * @SuppressWarnings(PHPMD.NPathComplexity)
120121 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
121122 */
122- public function initializeFromData (\ Magento \ Catalog \ Model \ Product $ product , array $ productData )
123+ public function initializeFromData (Product $ product , array $ productData )
123124 {
124125 unset($ productData ['custom_attributes ' ]);
125126 unset($ productData ['extension_attributes ' ]);
@@ -195,36 +196,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
195196 }
196197
197198 $ product = $ this ->setProductLinks ($ product );
198-
199- /**
200- * Initialize product options
201- */
202- if ($ productOptions && !$ product ->getOptionsReadonly ()) {
203- // mark custom options that should to fall back to default value
204- $ options = $ this ->mergeProductOptions (
205- $ productOptions ,
206- $ this ->request ->getPost ('options_use_default ' )
207- );
208- $ customOptions = [];
209- foreach ($ options as $ customOptionData ) {
210- if (empty ($ customOptionData ['is_delete ' ])) {
211- if (empty ($ customOptionData ['option_id ' ])) {
212- $ customOptionData ['option_id ' ] = null ;
213- }
214-
215- if (isset ($ customOptionData ['values ' ])) {
216- $ customOptionData ['values ' ] = array_filter ($ customOptionData ['values ' ], function ($ valueData ) {
217- return empty ($ valueData ['is_delete ' ]);
218- });
219- }
220-
221- $ customOption = $ this ->getCustomOptionFactory ()->create (['data ' => $ customOptionData ]);
222- $ customOption ->setProductSku ($ product ->getSku ());
223- $ customOptions [] = $ customOption ;
224- }
225- }
226- $ product ->setOptions ($ customOptions );
227- }
199+ $ product = $ this ->fillProductOptions ($ product , $ productOptions );
228200
229201 $ product ->setCanSaveCustomOptions (
230202 !empty ($ productData ['affect_product_custom_options ' ]) && !$ product ->getOptionsReadonly ()
@@ -236,10 +208,10 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
236208 /**
237209 * Initialize product before saving
238210 *
239- * @param \Magento\Catalog\Model\ Product $product
240- * @return \Magento\Catalog\Model\ Product
211+ * @param Product $product
212+ * @return Product
241213 */
242- public function initialize (\ Magento \ Catalog \ Model \ Product $ product )
214+ public function initialize (Product $ product )
243215 {
244216 $ productData = $ this ->request ->getPost ('product ' , []);
245217 return $ this ->initializeFromData ($ product , $ productData );
@@ -248,11 +220,11 @@ public function initialize(\Magento\Catalog\Model\Product $product)
248220 /**
249221 * Setting product links
250222 *
251- * @param \Magento\Catalog\Model\ Product $product
252- * @return \Magento\Catalog\Model\ Product
223+ * @param Product $product
224+ * @return Product
253225 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
254226 */
255- protected function setProductLinks (\ Magento \ Catalog \ Model \ Product $ product )
227+ protected function setProductLinks (Product $ product )
256228 {
257229 $ links = $ this ->getLinkResolver ()->getLinks ();
258230
@@ -454,4 +426,51 @@ private function getDateTimeFilter()
454426
455427 return $ this ->dateTimeFilter ;
456428 }
429+
430+ /**
431+ * Fills $product with options from $productOptions array
432+ *
433+ * @param Product $product
434+ * @param array $productOptions
435+ * @return Product
436+ */
437+ private function fillProductOptions (Product $ product , array $ productOptions )
438+ {
439+ if ($ product ->getOptionsReadonly ()) {
440+ return $ product ;
441+ }
442+ if (empty ($ productOptions )) {
443+ return $ product ->setOptions ([]);
444+ }
445+ // mark custom options that should to fall back to default value
446+ $ options = $ this ->mergeProductOptions (
447+ $ productOptions ,
448+ $ this ->request ->getPost ('options_use_default ' )
449+ );
450+ $ customOptions = [];
451+ foreach ($ options as $ customOptionData ) {
452+ if (!empty ($ customOptionData ['is_delete ' ])) {
453+ continue ;
454+ }
455+
456+ if (empty ($ customOptionData ['option_id ' ])) {
457+ $ customOptionData ['option_id ' ] = null ;
458+ }
459+ if (isset ($ customOptionData ['values ' ])) {
460+ $ customOptionData ['values ' ] = array_filter (
461+ $ customOptionData ['values ' ],
462+ function ($ valueData ) {
463+ return empty ($ valueData ['is_delete ' ]);
464+ }
465+ );
466+ }
467+ $ customOption = $ this ->getCustomOptionFactory ()->create (
468+ ['data ' => $ customOptionData ]
469+ );
470+ $ customOption ->setProductSku ($ product ->getSku ());
471+ $ customOptions [] = $ customOption ;
472+ }
473+
474+ return $ product ->setOptions ($ customOptions );
475+ }
457476}
0 commit comments