Skip to content

Commit 4ffd05d

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

File tree

1 file changed

+69
-93
lines changed
  • app/code/Magento/Catalog/Model/ResourceModel/Product

1 file changed

+69
-93
lines changed

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

Lines changed: 69 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
namespace Magento\Catalog\Model\ResourceModel\Product;
77

88
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Framework\DataObject;
10+
use Magento\Framework\Model\AbstractModel;
911
use Magento\Store\Model\Store;
1012

1113
/**
@@ -77,10 +79,10 @@ protected function _construct()
7779
/**
7880
* Save options store data
7981
*
80-
* @param \Magento\Framework\Model\AbstractModel $object
82+
* @param AbstractModel $object
8183
* @return \Magento\Framework\Model\ResourceModel\Db\AbstractDb
8284
*/
83-
protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
85+
protected function _afterSave(AbstractModel $object)
8486
{
8587
$this->_saveValuePrices($object);
8688
$this->_saveValueTitles($object);
@@ -91,66 +93,21 @@ protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
9193
/**
9294
* Save value prices
9395
*
94-
* @param \Magento\Framework\Model\AbstractModel $object
96+
* @param AbstractModel $object
9597
* @return $this
9698
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
9799
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
98100
*/
99-
protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $object)
101+
protected function _saveValuePrices(AbstractModel $object)
100102
{
101-
$priceTable = $this->getTable('catalog_product_option_price');
102-
$connection = $this->getConnection();
103-
104103
/*
105104
* Better to check param 'price' and 'price_type' for saving.
106105
* If there is not price skip saving price
107106
*/
108-
109107
if (in_array($object->getType(), $this->getPriceTypes())) {
110108
// save for store_id = 0
111109
if (!$object->getData('scope', 'price')) {
112-
$statement = $connection->select()->from(
113-
$priceTable,
114-
'option_id'
115-
)->where(
116-
'option_id = ?',
117-
$object->getId()
118-
)->where(
119-
'store_id = ?',
120-
Store::DEFAULT_STORE_ID
121-
);
122-
$optionId = $connection->fetchOne($statement);
123-
124-
if (!$optionId) {
125-
$data = $this->_prepareDataForTable(
126-
new \Magento\Framework\DataObject(
127-
[
128-
'option_id' => $object->getId(),
129-
'store_id' => Store::DEFAULT_STORE_ID,
130-
'price' => $object->getPrice(),
131-
'price_type' => $object->getPriceType(),
132-
]
133-
),
134-
$priceTable
135-
);
136-
$connection->insert($priceTable, $data);
137-
} elseif ((int)$object->getStoreId() === Store::DEFAULT_STORE_ID) {
138-
$data = $this->_prepareDataForTable(
139-
new \Magento\Framework\DataObject(
140-
['price' => $object->getPrice(), 'price_type' => $object->getPriceType()]
141-
),
142-
$priceTable
143-
);
144-
145-
$connection->update(
146-
$priceTable,
147-
$data,
148-
[
149-
'option_id = ?' => $object->getId(),
150-
'store_id = ?' => Store::DEFAULT_STORE_ID
151-
]
152-
);
153-
}
110+
$this->savePriceByStore($object, Store::DEFAULT_STORE_ID);
154111
}
155112

156113
$scope = (int)$this->_config->getValue(
@@ -178,49 +135,12 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
178135
$newPrice = $object->getPrice();
179136
}
180137

181-
$statement = $connection->select()->from(
182-
$priceTable
183-
)->where(
184-
'option_id = ?',
185-
$object->getId()
186-
)->where(
187-
'store_id = ?',
188-
$storeId
189-
);
190-
191-
if ($connection->fetchOne($statement)) {
192-
$data = $this->_prepareDataForTable(
193-
new \Magento\Framework\DataObject(
194-
['price' => $newPrice, 'price_type' => $object->getPriceType()]
195-
),
196-
$priceTable
197-
);
198-
199-
$connection->update(
200-
$priceTable,
201-
$data,
202-
['option_id = ?' => $object->getId(), 'store_id = ?' => $storeId]
203-
);
204-
} else {
205-
$data = $this->_prepareDataForTable(
206-
new \Magento\Framework\DataObject(
207-
[
208-
'option_id' => $object->getId(),
209-
'store_id' => $storeId,
210-
'price' => $newPrice,
211-
'price_type' => $object->getPriceType(),
212-
]
213-
),
214-
$priceTable
215-
);
216-
$connection->insert($priceTable, $data);
217-
}
138+
$this->savePriceByStore($object, (int)$storeId, $newPrice);
218139
}
219140
}
220-
} elseif ($scope == Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')
221-
) {
222-
$connection->delete(
223-
$priceTable,
141+
} elseif ($scope == Store::PRICE_SCOPE_WEBSITE && $object->getData('scope', 'price')) {
142+
$this->getConnection()->delete(
143+
$this->getTable('catalog_product_option_price'),
224144
['option_id = ?' => $object->getId(), 'store_id = ?' => $object->getStoreId()]
225145
);
226146
}
@@ -229,14 +149,68 @@ protected function _saveValuePrices(\Magento\Framework\Model\AbstractModel $obje
229149
return $this;
230150
}
231151

152+
/**
153+
* Save option price by store
154+
*
155+
* @param AbstractModel $object
156+
* @param int $storeId
157+
* @param float|null $newPrice
158+
*/
159+
private function savePriceByStore(AbstractModel $object, int $storeId, float $newPrice = null): void
160+
{
161+
$priceTable = $this->getTable('catalog_product_option_price');
162+
$connection = $this->getConnection();
163+
$price = $newPrice === null ? $object->getPrice() : $newPrice;
164+
165+
$statement = $connection->select()->from($priceTable, 'option_id')
166+
->where('option_id = ?', $object->getId())
167+
->where('store_id = ?', $storeId);
168+
$optionId = $connection->fetchOne($statement);
169+
170+
if (!$optionId) {
171+
$data = $this->_prepareDataForTable(
172+
new DataObject([
173+
'option_id' => $object->getId(),
174+
'store_id' => $storeId,
175+
'price' => $price,
176+
'price_type' => $object->getPriceType(),
177+
]),
178+
$priceTable
179+
);
180+
$connection->insert($priceTable, $data);
181+
} else {
182+
// skip to update the default price when the store price is saving
183+
if ($storeId === Store::DEFAULT_STORE_ID && (int)$object->getStoreId() !== $storeId) {
184+
return;
185+
}
186+
187+
$data = $this->_prepareDataForTable(
188+
new DataObject([
189+
'price' => $price,
190+
'price_type' => $object->getPriceType()
191+
]),
192+
$priceTable
193+
);
194+
195+
$connection->update(
196+
$priceTable,
197+
$data,
198+
[
199+
'option_id = ?' => $object->getId(),
200+
'store_id = ?' => $storeId
201+
]
202+
);
203+
}
204+
}
205+
232206
/**
233207
* Save titles
234208
*
235-
* @param \Magento\Framework\Model\AbstractModel $object
209+
* @param AbstractModel $object
236210
* @return void
237211
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
238212
*/
239-
protected function _saveValueTitles(\Magento\Framework\Model\AbstractModel $object)
213+
protected function _saveValueTitles(AbstractModel $object)
240214
{
241215
$connection = $this->getConnection();
242216
$titleTableName = $this->getTable('catalog_product_option_title');
@@ -583,6 +557,8 @@ public function getPriceTypes()
583557
}
584558

585559
/**
560+
* Get Metadata Pool
561+
*
586562
* @return \Magento\Framework\EntityManager\MetadataPool
587563
*/
588564
private function getMetadataPool()

0 commit comments

Comments
 (0)