Skip to content

Commit cbe5578

Browse files
author
Oleksii Korshenko
authored
Merge pull request #1644 from magento-engcom/2.1-develop-prs
Public Pull Requests #11844 Save the price 0 as price in custom options [backport 2.1] by @raumatbel #11728 Check variable existence in prepareOptionIds(array) in EavAttribute.php by @angelo983 Fixed Public Issues #4808 The price of product custom option can't be set to 0.
2 parents a518a12 + 66bd524 commit cbe5578

File tree

2 files changed

+62
-50
lines changed

2 files changed

+62
-50
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Option/Value.php

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,66 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product\Option;
77

8+
use Magento\Catalog\Model\Product\Option\Value as OptionValue;
9+
use Magento\Directory\Model\Currency;
10+
use Magento\Directory\Model\CurrencyFactory;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Locale\FormatInterface;
14+
use Magento\Framework\Model\AbstractModel;
15+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
16+
use Magento\Framework\Model\ResourceModel\Db\Context;
17+
use Magento\Store\Model\ScopeInterface;
18+
use Magento\Store\Model\Store;
19+
use Magento\Store\Model\StoreManagerInterface;
20+
821
/**
922
* Catalog product custom option resource model
1023
*
1124
* @author Magento Core Team <[email protected]>
1225
*/
13-
class Value extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
26+
class Value extends AbstractDb
1427
{
1528
/**
1629
* Store manager
1730
*
18-
* @var \Magento\Store\Model\StoreManagerInterface
31+
* @var StoreManagerInterface
1932
*/
2033
protected $_storeManager;
2134

2235
/**
2336
* Currency factory
2437
*
25-
* @var \Magento\Directory\Model\CurrencyFactory
38+
* @var CurrencyFactory
2639
*/
2740
protected $_currencyFactory;
2841

2942
/**
3043
* Core config model
3144
*
32-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
45+
* @var ScopeConfigInterface
3346
*/
3447
protected $_config;
3548

3649
/**
37-
* @var \Magento\Framework\Locale\FormatInterface
50+
* @var FormatInterface
3851
*/
3952
private $localeFormat;
4053

4154
/**
4255
* Class constructor
4356
*
44-
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
45-
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
46-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
47-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
57+
* @param Context $context
58+
* @param CurrencyFactory $currencyFactory
59+
* @param StoreManagerInterface $storeManager
60+
* @param ScopeConfigInterface $config
4861
* @param string $connectionName
4962
*/
5063
public function __construct(
51-
\Magento\Framework\Model\ResourceModel\Db\Context $context,
52-
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
53-
\Magento\Store\Model\StoreManagerInterface $storeManager,
54-
\Magento\Framework\App\Config\ScopeConfigInterface $config,
64+
Context $context,
65+
CurrencyFactory $currencyFactory,
66+
StoreManagerInterface $storeManager,
67+
ScopeConfigInterface $config,
5568
$connectionName = null
5669
) {
5770
$this->_currencyFactory = $currencyFactory;
@@ -74,10 +87,10 @@ protected function _construct()
7487
* Proceed operations after object is saved
7588
* Save options store data
7689
*
77-
* @param \Magento\Framework\Model\AbstractModel $object
78-
* @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
90+
* @param AbstractModel $object
91+
* @return AbstractDb
7992
*/
80-
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
93+
protected function _afterSave(AbstractModel $object)
8194
{
8295
$this->_saveValuePrices($object);
8396
$this->_saveValueTitles($object);
@@ -88,20 +101,21 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
88101
/**
89102
* Save option value price data.
90103
*
91-
* @param \Magento\Framework\Model\AbstractModel $object
104+
* @param AbstractModel $object
92105
* @return void
93106
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
94107
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
95108
*/
96-
protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $object)
109+
protected function _saveValuePrices(AbstractModel $object)
97110
{
111+
$objectPrice = $object->getPrice();
98112
$priceTable = $this->getTable('catalog_product_option_type_price');
99-
$formattedPrice = $this->getLocaleFormatter()->getNumber($object->getPrice());
113+
$formattedPrice = $this->getLocaleFormatter()->getNumber($objectPrice);
100114

101115
$price = (double)sprintf('%F', $formattedPrice);
102116
$priceType = $object->getPriceType();
103117

104-
if ($object->getPrice() && $priceType) {
118+
if (isset($objectPrice) && $priceType) {
105119
//save for store_id = 0
106120
$select = $this->getConnection()->select()->from(
107121
$priceTable,
@@ -111,7 +125,7 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
111125
(int)$object->getId()
112126
)->where(
113127
'store_id = ?',
114-
\Magento\Store\Model\Store::DEFAULT_STORE_ID
128+
Store::DEFAULT_STORE_ID
115129
);
116130
$optionTypeId = $this->getConnection()->fetchOne($select);
117131

@@ -120,15 +134,15 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
120134
$bind = ['price' => $price, 'price_type' => $priceType];
121135
$where = [
122136
'option_type_id = ?' => $optionTypeId,
123-
'store_id = ?' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
137+
'store_id = ?' => Store::DEFAULT_STORE_ID,
124138
];
125139

126140
$this->getConnection()->update($priceTable, $bind, $where);
127141
}
128142
} else {
129143
$bind = [
130144
'option_type_id' => (int)$object->getId(),
131-
'store_id' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
145+
'store_id' => Store::DEFAULT_STORE_ID,
132146
'price' => $price,
133147
'price_type' => $priceType,
134148
];
@@ -137,17 +151,17 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
137151
}
138152

139153
$scope = (int)$this->_config->getValue(
140-
\Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE,
141-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
154+
Store::XML_PATH_PRICE_SCOPE,
155+
ScopeInterface::SCOPE_STORE
142156
);
143157

144-
if ($scope == \Magento\Store\Model\Store::PRICE_SCOPE_WEBSITE
158+
if ($scope == Store::PRICE_SCOPE_WEBSITE
145159
&& $priceType
146-
&& $object->getPrice()
147-
&& $object->getStoreId() != \Magento\Store\Model\Store::DEFAULT_STORE_ID
160+
&& isset($objectPrice)
161+
&& $object->getStoreId() != Store::DEFAULT_STORE_ID
148162
) {
149163
$baseCurrency = $this->_config->getValue(
150-
\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
164+
Currency::XML_PATH_CURRENCY_BASE,
151165
'default'
152166
);
153167

@@ -156,7 +170,7 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
156170
foreach ($storeIds as $storeId) {
157171
if ($priceType == 'fixed') {
158172
$storeCurrency = $this->_storeManager->getStore($storeId)->getBaseCurrencyCode();
159-
/** @var $currencyModel \Magento\Directory\Model\Currency */
173+
/** @var $currencyModel Currency */
160174
$currencyModel = $this->_currencyFactory->create();
161175
$currencyModel->load($baseCurrency);
162176
$rate = $currencyModel->getRate($storeCurrency);
@@ -198,8 +212,8 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
198212
}
199213
}
200214
} else {
201-
if ($scope == \Magento\Store\Model\Store::PRICE_SCOPE_WEBSITE
202-
&& !$object->getPrice()
215+
if ($scope == Store::PRICE_SCOPE_WEBSITE
216+
&& !isset($objectPrice)
203217
&& !$priceType
204218
) {
205219
$storeIds = $this->_storeManager->getStore($object->getStoreId())->getWebsite()->getStoreIds();
@@ -217,13 +231,13 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
217231
/**
218232
* Save option value title data
219233
*
220-
* @param \Magento\Framework\Model\AbstractModel $object
234+
* @param AbstractModel $object
221235
* @return void
222236
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
223237
*/
224-
protected function _saveValueTitles(\Magento\Framework\Model\AbstractModel $object)
238+
protected function _saveValueTitles(AbstractModel $object)
225239
{
226-
foreach ([\Magento\Store\Model\Store::DEFAULT_STORE_ID, $object->getStoreId()] as $storeId) {
240+
foreach ([Store::DEFAULT_STORE_ID, $object->getStoreId()] as $storeId) {
227241
$titleTable = $this->getTable('catalog_product_option_type_title');
228242
$select = $this->getConnection()->select()->from(
229243
$titleTable,
@@ -238,9 +252,7 @@ protected function _saveValueTitles(\Magento\Framework\Model\AbstractModel $obje
238252
$optionTypeId = $this->getConnection()->fetchOne($select);
239253
$existInCurrentStore = $this->getOptionIdFromOptionTable($titleTable, (int)$object->getId(), (int)$storeId);
240254

241-
if ($storeId != \Magento\Store\Model\Store::DEFAULT_STORE_ID &&
242-
$object->getData('is_delete_store_title')
243-
) {
255+
if ($storeId != Store::DEFAULT_STORE_ID && $object->getData('is_delete_store_title')) {
244256
$object->unsetData('title');
245257
}
246258

@@ -258,11 +270,11 @@ protected function _saveValueTitles(\Magento\Framework\Model\AbstractModel $obje
258270
$existInDefaultStore = $this->getOptionIdFromOptionTable(
259271
$titleTable,
260272
(int)$object->getId(),
261-
\Magento\Store\Model\Store::DEFAULT_STORE_ID
273+
Store::DEFAULT_STORE_ID
262274
);
263275
// we should insert record into not default store only of if it does not exist in default store
264-
if (($storeId == \Magento\Store\Model\Store::DEFAULT_STORE_ID && !$existInDefaultStore)
265-
|| ($storeId != \Magento\Store\Model\Store::DEFAULT_STORE_ID && !$existInCurrentStore)
276+
if (($storeId == Store::DEFAULT_STORE_ID && !$existInDefaultStore)
277+
|| ($storeId != Store::DEFAULT_STORE_ID && !$existInCurrentStore)
266278
) {
267279
$bind = [
268280
'option_type_id' => (int)$object->getId(),
@@ -275,7 +287,7 @@ protected function _saveValueTitles(\Magento\Framework\Model\AbstractModel $obje
275287
} else {
276288
if ($storeId
277289
&& $optionTypeId
278-
&& $object->getStoreId() > \Magento\Store\Model\Store::DEFAULT_STORE_ID
290+
&& $object->getStoreId() > Store::DEFAULT_STORE_ID
279291
) {
280292
$where = [
281293
'option_type_id = ?' => (int)$optionTypeId,
@@ -355,12 +367,12 @@ public function deleteValues($optionTypeId)
355367
/**
356368
* Duplicate product options value
357369
*
358-
* @param \Magento\Catalog\Model\Product\Option\Value $object
370+
* @param OptionValue $object
359371
* @param int $oldOptionId
360372
* @param int $newOptionId
361-
* @return \Magento\Catalog\Model\Product\Option\Value
373+
* @return OptionValue
362374
*/
363-
public function duplicate(\Magento\Catalog\Model\Product\Option\Value $object, $oldOptionId, $newOptionId)
375+
public function duplicate(OptionValue $object, $oldOptionId, $newOptionId)
364376
{
365377
$connection = $this->getConnection();
366378
$select = $connection->select()->from($this->getMainTable())->where('option_id = ?', $oldOptionId);
@@ -427,16 +439,16 @@ public function duplicate(\Magento\Catalog\Model\Product\Option\Value $object, $
427439
/**
428440
* Get FormatInterface to convert price from string to number format.
429441
*
430-
* @return \Magento\Framework\Locale\FormatInterface
442+
* @return FormatInterface
431443
* @deprecated
432444
*/
433445
private function getLocaleFormatter()
434446
{
435447
if ($this->localeFormat === null) {
436-
$this->localeFormat = \Magento\Framework\App\ObjectManager::getInstance()
437-
->get(\Magento\Framework\Locale\FormatInterface::class);
448+
$this->localeFormat = ObjectManager::getInstance()
449+
->get(FormatInterface::class);
438450
}
439-
451+
440452
return $this->localeFormat;
441453
}
442454
}

app/code/Magento/Swatches/Model/Plugin/EavAttribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected function prepareOptionIds(array $optionsArray)
174174
{
175175
if (isset($optionsArray['value']) && is_array($optionsArray['value'])) {
176176
foreach (array_keys($optionsArray['value']) as $optionId) {
177-
if (isset($optionsArray['delete']) && $optionsArray['delete'][$optionId] == 1) {
177+
if (isset($optionsArray['delete'][$optionId]) && $optionsArray['delete'][$optionId] == 1) {
178178
unset($optionsArray['value'][$optionId]);
179179
}
180180
}

0 commit comments

Comments
 (0)