Skip to content

Commit 98bc420

Browse files
author
Timon de Groot
committed
Fix fatal error when parsing calculating localized option price on product save
1 parent db0dce6 commit 98bc420

File tree

1 file changed

+50
-30
lines changed
  • app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization

1 file changed

+50
-30
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Initialization/Helper.php

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@
66

77
namespace Magento\Catalog\Controller\Adminhtml\Product\Initialization;
88

9+
use DateTime;
10+
use Magento\Backend\Helper\Js;
911
use Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory as CustomOptionFactory;
1012
use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory as ProductLinkFactory;
13+
use Magento\Catalog\Api\Data\ProductLinkTypeInterface;
1114
use Magento\Catalog\Api\ProductRepositoryInterface;
1215
use 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;
1417
use Magento\Catalog\Model\Product;
1518
use Magento\Catalog\Model\Product\Initialization\Helper\ProductLinks;
1619
use Magento\Catalog\Model\Product\Link\Resolver as LinkResolver;
1720
use Magento\Catalog\Model\Product\LinkTypeProvider;
1821
use 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
@@ -28,12 +35,12 @@
2835
class 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'])) {
@@ -222,10 +237,10 @@ 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', []);
231246
return $this->initializeFromData($product, $productData);
@@ -234,12 +249,12 @@ public function initialize(\Magento\Catalog\Model\Product $product)
234249
/**
235250
* Setting product links
236251
*
237-
* @param \Magento\Catalog\Model\Product $product
238-
* @return \Magento\Catalog\Model\Product
252+
* @param Product $product
253+
* @return Product
239254
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
240255
* @since 101.0.0
241256
*/
242-
protected function setProductLinks(\Magento\Catalog\Model\Product $product)
257+
protected function setProductLinks(Product $product)
243258
{
244259
$links = $this->getLinkResolver()->getLinks();
245260

@@ -249,7 +264,7 @@ protected function setProductLinks(\Magento\Catalog\Model\Product $product)
249264
$productLinks = $product->getProductLinks();
250265
$linkTypes = [];
251266

252-
/** @var \Magento\Catalog\Api\Data\ProductLinkTypeInterface $linkTypeObject */
267+
/** @var ProductLinkTypeInterface $linkTypeObject */
253268
foreach ($this->linkTypeProvider->getItems() as $linkTypeObject) {
254269
$linkTypes[$linkTypeObject->getName()] = $product->getData($linkTypeObject->getName() . '_readonly');
255270
}
@@ -389,7 +404,7 @@ private function getLinkResolver()
389404
private function getDateTimeFilter()
390405
{
391406
if ($this->dateTimeFilter === null) {
392-
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
407+
$this->dateTimeFilter = ObjectManager::getInstance()
393408
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
394409
}
395410
return $this->dateTimeFilter;
@@ -453,6 +468,11 @@ private function fillProductOptions(Product $product, array $productOptions)
453468
});
454469
}
455470

471+
if (isset($customOptionData['price'])) {
472+
// Make sure we're working with a number here and no localized value.
473+
$customOptionData['price'] = $this->localeFormat->getNumber($customOptionData['price']);
474+
}
475+
456476
$customOption = $this->customOptionFactory->create(['data' => $customOptionData]);
457477
$customOption->setProductSku($product->getSku());
458478
$customOptions[] = $customOption;
@@ -471,7 +491,7 @@ private function convertSpecialFromDateStringToObject($productData)
471491
{
472492
if (isset($productData['special_from_date']) && $productData['special_from_date'] != '') {
473493
$productData['special_from_date'] = $this->getDateTimeFilter()->filter($productData['special_from_date']);
474-
$productData['special_from_date'] = new \DateTime($productData['special_from_date']);
494+
$productData['special_from_date'] = new DateTime($productData['special_from_date']);
475495
}
476496

477497
return $productData;

0 commit comments

Comments
 (0)