Skip to content

Commit 0214b3d

Browse files
committed
MAGETWO-99587: Custom options price from website scope rewrites prices on all scopes
1 parent 4ffd05d commit 0214b3d

File tree

2 files changed

+44
-33
lines changed
  • app/code/Magento/Catalog/Model/ResourceModel/Product
  • dev/tests/integration/testsuite/Magento/Catalog/Model/Product

2 files changed

+44
-33
lines changed

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

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\DataObject;
1010
use Magento\Framework\Model\AbstractModel;
1111
use Magento\Store\Model\Store;
12+
use Magento\Tests\NamingConvention\true\float;
1213

1314
/**
1415
* Catalog product custom option resource model
@@ -95,8 +96,6 @@ protected function _afterSave(AbstractModel $object)
9596
*
9697
* @param AbstractModel $object
9798
* @return $this
98-
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
99-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
10099
*/
101100
protected function _saveValuePrices(AbstractModel $object)
102101
{
@@ -116,27 +115,13 @@ protected function _saveValuePrices(AbstractModel $object)
116115
);
117116

118117
if ($object->getStoreId() != '0' && $scope == Store::PRICE_SCOPE_WEBSITE) {
119-
$baseCurrency = $this->_config->getValue(
120-
\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
121-
'default'
122-
);
123-
124118
$storeIds = $this->_storeManager->getStore($object->getStoreId())->getWebsite()->getStoreIds();
125-
if (is_array($storeIds)) {
126-
foreach ($storeIds as $storeId) {
127-
if ($object->getPriceType() == 'fixed') {
128-
$storeCurrency = $this->_storeManager->getStore($storeId)->getBaseCurrencyCode();
129-
$rate = $this->_currencyFactory->create()->load($baseCurrency)->getRate($storeCurrency);
130-
if (!$rate) {
131-
$rate = 1;
132-
}
133-
$newPrice = $object->getPrice() * $rate;
134-
} else {
135-
$newPrice = $object->getPrice();
136-
}
137-
138-
$this->savePriceByStore($object, (int)$storeId, $newPrice);
139-
}
119+
if (empty($storeIds)) {
120+
return $this;
121+
}
122+
foreach ($storeIds as $storeId) {
123+
$newPrice = $this->calculateStorePrice($object, $storeId);
124+
$this->savePriceByStore($object, (int)$storeId, $newPrice);
140125
}
141126
} elseif ($scope == Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')) {
142127
$this->getConnection()->delete(
@@ -169,12 +154,14 @@ private function savePriceByStore(AbstractModel $object, int $storeId, float $ne
169154

170155
if (!$optionId) {
171156
$data = $this->_prepareDataForTable(
172-
new DataObject([
173-
'option_id' => $object->getId(),
174-
'store_id' => $storeId,
175-
'price' => $price,
176-
'price_type' => $object->getPriceType(),
177-
]),
157+
new DataObject(
158+
[
159+
'option_id' => $object->getId(),
160+
'store_id' => $storeId,
161+
'price' => $price,
162+
'price_type' => $object->getPriceType(),
163+
]
164+
),
178165
$priceTable
179166
);
180167
$connection->insert($priceTable, $data);
@@ -185,10 +172,12 @@ private function savePriceByStore(AbstractModel $object, int $storeId, float $ne
185172
}
186173

187174
$data = $this->_prepareDataForTable(
188-
new DataObject([
189-
'price' => $price,
190-
'price_type' => $object->getPriceType()
191-
]),
175+
new DataObject(
176+
[
177+
'price' => $price,
178+
'price_type' => $object->getPriceType()
179+
]
180+
),
192181
$priceTable
193182
);
194183

@@ -203,6 +192,29 @@ private function savePriceByStore(AbstractModel $object, int $storeId, float $ne
203192
}
204193
}
205194

195+
/**
196+
* Calculate price by store
197+
*
198+
* @param AbstractModel $object
199+
* @param int $storeId
200+
* @return float
201+
*/
202+
private function calculateStorePrice(AbstractModel $object, int $storeId): float
203+
{
204+
$price = $object->getPrice();
205+
if ($object->getPriceType() == 'fixed') {
206+
$baseCurrency = $this->_config->getValue(
207+
\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
208+
'default'
209+
);
210+
$storeCurrency = $this->_storeManager->getStore($storeId)->getBaseCurrencyCode();
211+
$rate = $this->_currencyFactory->create()->load($baseCurrency)->getRate($storeCurrency);
212+
$price = $object->getPrice() * ($rate ?: 1);
213+
}
214+
215+
return (float)$price;
216+
}
217+
206218
/**
207219
* Save titles
208220
*

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/OptionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class OptionTest extends \PHPUnit\Framework\TestCase
3838
*/
3939
private $storeManager;
4040

41-
4241
/**
4342
* @inheritdoc
4443
*/

0 commit comments

Comments
 (0)