66
77namespace Magento \Catalog \Controller \Adminhtml \Product \Initialization ;
88
9+ use DateTime ;
10+ use Magento \Backend \Helper \Js ;
911use Magento \Catalog \Api \Data \ProductCustomOptionInterfaceFactory as CustomOptionFactory ;
1012use Magento \Catalog \Api \Data \ProductLinkInterfaceFactory as ProductLinkFactory ;
13+ use Magento \Catalog \Api \Data \ProductLinkTypeInterface ;
1114use Magento \Catalog \Api \ProductRepositoryInterface ;
1215use Magento \Catalog \Api \ProductRepositoryInterface \Proxy as ProductRepository ;
13- use Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper \AttributeDefaultValueFilter ;
16+ use Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper \AttributeFilter ;
1417use Magento \Catalog \Model \Product ;
1518use Magento \Catalog \Model \Product \Initialization \Helper \ProductLinks ;
1619use Magento \Catalog \Model \Product \Link \Resolver as LinkResolver ;
1720use Magento \Catalog \Model \Product \LinkTypeProvider ;
1821use Magento \Framework \App \ObjectManager ;
19- use Magento \Catalog \Controller \Adminhtml \Product \Initialization \Helper \AttributeFilter ;
22+ use Magento \Framework \App \RequestInterface ;
23+ use Magento \Framework \Locale \FormatInterface ;
24+ use Magento \Framework \Stdlib \DateTime \Filter \Date ;
25+ use Magento \Store \Model \StoreManagerInterface ;
26+ use Zend_Filter_Input ;
2027
2128/**
2229 * Product helper
2835class Helper
2936{
3037 /**
31- * @var \Magento\Framework\App\ RequestInterface
38+ * @var RequestInterface
3239 */
3340 protected $ request ;
3441
3542 /**
36- * @var \Magento\Store\Model\ StoreManagerInterface
43+ * @var StoreManagerInterface
3744 */
3845 protected $ storeManager ;
3946
@@ -43,12 +50,12 @@ class Helper
4350 protected $ stockFilter ;
4451
4552 /**
46- * @var \Magento\Backend\Helper\ Js
53+ * @var Js
4754 */
4855 protected $ jsHelper ;
4956
5057 /**
51- * @var \Magento\Framework\Stdlib\DateTime\Filter\ Date
58+ * @var Date
5259 * @deprecated 101.0.0
5360 */
5461 protected $ dateFilter ;
@@ -96,34 +103,41 @@ class Helper
96103 */
97104 private $ attributeFilter ;
98105
106+ /**
107+ * @var FormatInterface
108+ */
109+ private $ localeFormat ;
110+
99111 /**
100112 * Constructor
101113 *
102- * @param \Magento\Framework\App\ RequestInterface $request
103- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
114+ * @param RequestInterface $request
115+ * @param StoreManagerInterface $storeManager
104116 * @param StockDataFilter $stockFilter
105117 * @param ProductLinks $productLinks
106- * @param \Magento\Backend\Helper\ Js $jsHelper
107- * @param \Magento\Framework\Stdlib\DateTime\Filter\ Date $dateFilter
118+ * @param Js $jsHelper
119+ * @param Date $dateFilter
108120 * @param CustomOptionFactory|null $customOptionFactory
109121 * @param ProductLinkFactory|null $productLinkFactory
110122 * @param ProductRepositoryInterface|null $productRepository
111123 * @param LinkTypeProvider|null $linkTypeProvider
112124 * @param AttributeFilter|null $attributeFilter
125+ * @param FormatInterface|null $localeFormat
113126 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
114127 */
115128 public function __construct (
116- \ Magento \ Framework \ App \ RequestInterface $ request ,
117- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
129+ RequestInterface $ request ,
130+ StoreManagerInterface $ storeManager ,
118131 StockDataFilter $ stockFilter ,
119- \ Magento \ Catalog \ Model \ Product \ Initialization \ Helper \ ProductLinks $ productLinks ,
120- \ Magento \ Backend \ Helper \ Js $ jsHelper ,
121- \ Magento \ Framework \ Stdlib \ DateTime \ Filter \ Date $ dateFilter ,
132+ ProductLinks $ productLinks ,
133+ Js $ jsHelper ,
134+ Date $ dateFilter ,
122135 CustomOptionFactory $ customOptionFactory = null ,
123136 ProductLinkFactory $ productLinkFactory = null ,
124137 ProductRepositoryInterface $ productRepository = null ,
125138 LinkTypeProvider $ linkTypeProvider = null ,
126- AttributeFilter $ attributeFilter = null
139+ AttributeFilter $ attributeFilter = null ,
140+ FormatInterface $ localeFormat = null
127141 ) {
128142 $ this ->request = $ request ;
129143 $ this ->storeManager = $ storeManager ;
@@ -132,26 +146,27 @@ public function __construct(
132146 $ this ->jsHelper = $ jsHelper ;
133147 $ this ->dateFilter = $ dateFilter ;
134148
135- $ objectManager = \ Magento \ Framework \ App \ ObjectManager::getInstance ();
149+ $ objectManager = ObjectManager::getInstance ();
136150 $ this ->customOptionFactory = $ customOptionFactory ?: $ objectManager ->get (CustomOptionFactory::class);
137151 $ this ->productLinkFactory = $ productLinkFactory ?: $ objectManager ->get (ProductLinkFactory::class);
138152 $ this ->productRepository = $ productRepository ?: $ objectManager ->get (ProductRepositoryInterface::class);
139153 $ this ->linkTypeProvider = $ linkTypeProvider ?: $ objectManager ->get (LinkTypeProvider::class);
140154 $ this ->attributeFilter = $ attributeFilter ?: $ objectManager ->get (AttributeFilter::class);
155+ $ this ->localeFormat = $ localeFormat ?: $ objectManager ->get (FormatInterface::class);
141156 }
142157
143158 /**
144159 * Initialize product from data
145160 *
146- * @param \Magento\Catalog\Model\ Product $product
161+ * @param Product $product
147162 * @param array $productData
148- * @return \Magento\Catalog\Model\ Product
163+ * @return Product
149164 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
150165 * @SuppressWarnings(PHPMD.NPathComplexity)
151166 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
152167 * @since 101.0.0
153168 */
154- public function initializeFromData (\ Magento \ Catalog \ Model \ Product $ product , array $ productData )
169+ public function initializeFromData (Product $ product , array $ productData )
155170 {
156171 unset($ productData ['custom_attributes ' ], $ productData ['extension_attributes ' ]);
157172
@@ -190,7 +205,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
190205 }
191206 }
192207
193- $ inputFilter = new \ Zend_Filter_Input ($ dateFieldFilters , [], $ productData );
208+ $ inputFilter = new Zend_Filter_Input ($ dateFieldFilters , [], $ productData );
194209 $ productData = $ inputFilter ->getUnescaped ();
195210
196211 if (isset ($ productData ['options ' ])) {
@@ -201,7 +216,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
201216 }
202217 $ productData ['tier_price ' ] = isset ($ productData ['tier_price ' ]) ? $ productData ['tier_price ' ] : [];
203218
204- $ useDefaults = (array )$ this ->request ->getPost ('use_default ' , []);
219+ $ useDefaults = (array ) $ this ->request ->getPost ('use_default ' , []);
205220 $ productData = $ this ->attributeFilter ->prepareProductAttributes ($ product , $ productData , $ useDefaults );
206221 $ product ->addData ($ productData );
207222
@@ -222,24 +237,25 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
222237 /**
223238 * Initialize product before saving
224239 *
225- * @param \Magento\Catalog\Model\ Product $product
226- * @return \Magento\Catalog\Model\ Product
240+ * @param Product $product
241+ * @return Product
227242 */
228- public function initialize (\ Magento \ Catalog \ Model \ Product $ product )
243+ public function initialize (Product $ product )
229244 {
230245 $ productData = $ this ->request ->getPost ('product ' , []);
246+
231247 return $ this ->initializeFromData ($ product , $ productData );
232248 }
233249
234250 /**
235251 * Setting product links
236252 *
237- * @param \Magento\Catalog\Model\ Product $product
238- * @return \Magento\Catalog\Model\ Product
253+ * @param Product $product
254+ * @return Product
239255 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
240256 * @since 101.0.0
241257 */
242- protected function setProductLinks (\ Magento \ Catalog \ Model \ Product $ product )
258+ protected function setProductLinks (Product $ product )
243259 {
244260 $ links = $ this ->getLinkResolver ()->getLinks ();
245261
@@ -249,7 +265,7 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
249265 $ productLinks = $ product ->getProductLinks ();
250266 $ linkTypes = [];
251267
252- /** @var \Magento\Catalog\Api\Data\ ProductLinkTypeInterface $linkTypeObject */
268+ /** @var ProductLinkTypeInterface $linkTypeObject */
253269 foreach ($ this ->linkTypeProvider ->getItems () as $ linkTypeObject ) {
254270 $ linkTypes [$ linkTypeObject ->getName ()] = $ product ->getData ($ linkTypeObject ->getName () . '_readonly ' );
255271 }
@@ -261,7 +277,7 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
261277
262278 foreach ($ linkTypes as $ linkType => $ readonly ) {
263279 if (isset ($ links [$ linkType ]) && !$ readonly ) {
264- foreach ((array )$ links [$ linkType ] as $ linkData ) {
280+ foreach ((array ) $ links [$ linkType ] as $ linkData ) {
265281 if (empty ($ linkData ['id ' ])) {
266282 continue ;
267283 }
@@ -271,7 +287,7 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
271287 $ link ->setSku ($ product ->getSku ())
272288 ->setLinkedProductSku ($ linkProduct ->getSku ())
273289 ->setLinkType ($ linkType )
274- ->setPosition (isset ($ linkData ['position ' ]) ? (int )$ linkData ['position ' ] : 0 );
290+ ->setPosition (isset ($ linkData ['position ' ]) ? (int ) $ linkData ['position ' ] : 0 );
275291 $ productLinks [] = $ link ;
276292 }
277293 }
@@ -377,6 +393,7 @@ private function getLinkResolver()
377393 if (!is_object ($ this ->linkResolver )) {
378394 $ this ->linkResolver = ObjectManager::getInstance ()->get (LinkResolver::class);
379395 }
396+
380397 return $ this ->linkResolver ;
381398 }
382399
@@ -389,9 +406,10 @@ private function getLinkResolver()
389406 private function getDateTimeFilter ()
390407 {
391408 if ($ this ->dateTimeFilter === null ) {
392- $ this ->dateTimeFilter = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
409+ $ this ->dateTimeFilter = ObjectManager::getInstance ()
393410 ->get (\Magento \Framework \Stdlib \DateTime \Filter \DateTime::class);
394411 }
412+
395413 return $ this ->dateTimeFilter ;
396414 }
397415
@@ -407,7 +425,7 @@ private function getDateTimeFilter()
407425 private function filterWebsiteIds ($ websiteIds )
408426 {
409427 if (!$ this ->storeManager ->isSingleStoreMode ()) {
410- $ websiteIds = array_filter ((array )$ websiteIds );
428+ $ websiteIds = array_filter ((array ) $ websiteIds );
411429 } else {
412430 $ websiteIds [$ this ->storeManager ->getWebsite (true )->getId ()] = 1 ;
413431 }
@@ -448,9 +466,17 @@ private function fillProductOptions(Product $product, array $productOptions)
448466 }
449467
450468 if (isset ($ customOptionData ['values ' ])) {
451- $ customOptionData ['values ' ] = array_filter ($ customOptionData ['values ' ], function ($ valueData ) {
452- return empty ($ valueData ['is_delete ' ]);
453- });
469+ $ customOptionData ['values ' ] = array_filter (
470+ $ customOptionData ['values ' ],
471+ function ($ valueData ) {
472+ return empty ($ valueData ['is_delete ' ]);
473+ }
474+ );
475+ }
476+
477+ if (isset ($ customOptionData ['price ' ])) {
478+ // Make sure we're working with a number here and no localized value.
479+ $ customOptionData ['price ' ] = $ this ->localeFormat ->getNumber ($ customOptionData ['price ' ]);
454480 }
455481
456482 $ customOption = $ this ->customOptionFactory ->create (['data ' => $ customOptionData ]);
@@ -471,7 +497,7 @@ private function convertSpecialFromDateStringToObject($productData)
471497 {
472498 if (isset ($ productData ['special_from_date ' ]) && $ productData ['special_from_date ' ] != '' ) {
473499 $ productData ['special_from_date ' ] = $ this ->getDateTimeFilter ()->filter ($ productData ['special_from_date ' ]);
474- $ productData ['special_from_date ' ] = new \ DateTime ($ productData ['special_from_date ' ]);
500+ $ productData ['special_from_date ' ] = new DateTime ($ productData ['special_from_date ' ]);
475501 }
476502
477503 return $ productData ;
0 commit comments