12
12
use Magento \Customer \Api \GroupRepositoryInterface ;
13
13
use Magento \Framework \Exception \CouldNotSaveException ;
14
14
use Magento \Framework \Exception \InputException ;
15
+ use Magento \Framework \Exception \LocalizedException ;
15
16
use Magento \Framework \Exception \TemporaryStateExceptionInterface ;
17
+ use Magento \Store \Api \Data \WebsiteInterface ;
18
+ use Magento \Store \Model \ScopeInterface ;
16
19
17
20
/**
18
- * Product tier price management
19
- *
20
21
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
22
*/
22
23
class TierPriceManagement implements \Magento \Catalog \Api \ProductTierPriceManagementInterface
@@ -100,7 +101,7 @@ public function add($sku, $customerGroupId, $price, $qty)
100
101
$ product = $ this ->productRepository ->get ($ sku , ['edit_mode ' => true ]);
101
102
$ tierPrices = $ product ->getData ('tier_price ' );
102
103
$ websiteIdentifier = 0 ;
103
- $ value = $ this ->config ->getValue ('catalog/price/scope ' , \ Magento \ Store \ Model \ ScopeInterface::SCOPE_WEBSITE );
104
+ $ value = $ this ->config ->getValue ('catalog/price/scope ' , ScopeInterface::SCOPE_WEBSITE );
104
105
if ($ value != 0 ) {
105
106
$ websiteIdentifier = $ this ->storeManager ->getWebsite ()->getId ();
106
107
}
@@ -160,9 +161,8 @@ public function remove($sku, $customerGroupId, $qty)
160
161
{
161
162
$ product = $ this ->productRepository ->get ($ sku , ['edit_mode ' => true ]);
162
163
$ websiteIdentifier = 0 ;
163
- $ value = $ this ->config ->getValue ('catalog/price/scope ' , \Magento \Store \Model \ScopeInterface::SCOPE_WEBSITE );
164
- if ($ value != 0 ) {
165
- $ websiteIdentifier = $ this ->storeManager ->getWebsite ()->getId ();
164
+ if ($ this ->getPriceScopeConfig () !== 0 ) {
165
+ $ websiteIdentifier = $ this ->getCurrentWebsite ()->getId ();
166
166
}
167
167
$ this ->priceModifier ->removeTierPrice ($ product , $ customerGroupId , $ qty , $ websiteIdentifier );
168
168
return true ;
@@ -175,23 +175,17 @@ public function getList($sku, $customerGroupId)
175
175
{
176
176
$ product = $ this ->productRepository ->get ($ sku , ['edit_mode ' => true ]);
177
177
178
- $ priceKey = 'website_price ' ;
179
- $ value = $ this ->config ->getValue ('catalog/price/scope ' , \Magento \Store \Model \ScopeInterface::SCOPE_WEBSITE );
180
- if ($ value == 0 ) {
181
- $ priceKey = 'price ' ;
182
- }
183
-
184
- $ cgi = ($ customerGroupId === 'all '
178
+ $ cgi = $ customerGroupId === 'all '
185
179
? $ this ->groupManagement ->getAllCustomersGroup ()->getId ()
186
- : $ customerGroupId) ;
180
+ : $ customerGroupId ;
187
181
188
182
$ prices = [];
189
183
$ tierPrices = $ product ->getData ('tier_price ' );
190
184
if ($ tierPrices !== null ) {
185
+ $ priceKey = $ this ->getPriceKey ();
186
+
191
187
foreach ($ tierPrices as $ price ) {
192
- if ((is_numeric ($ customerGroupId ) && (int ) $ price ['cust_group ' ] === (int ) $ customerGroupId )
193
- || ($ customerGroupId === 'all ' && $ price ['all_groups ' ])
194
- ) {
188
+ if ($ this ->isCustomerGroupApplicable ($ customerGroupId , $ price )) {
195
189
/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterface $tierPrice */
196
190
$ tierPrice = $ this ->priceFactory ->create ();
197
191
$ tierPrice ->setValue ($ price [$ priceKey ])
@@ -203,4 +197,48 @@ public function getList($sku, $customerGroupId)
203
197
}
204
198
return $ prices ;
205
199
}
200
+
201
+ /**
202
+ * Returns attribute code (key) that contains price
203
+ *
204
+ * @return string
205
+ */
206
+ private function getPriceKey (): string
207
+ {
208
+ return $ this ->getPriceScopeConfig () === 0 ? 'price ' : 'website_price ' ;
209
+ }
210
+
211
+ /**
212
+ * Returns whether Price is applicable for provided Customer Group
213
+ *
214
+ * @param string $customerGroupId
215
+ * @param array $priceArray
216
+ * @return bool
217
+ */
218
+ private function isCustomerGroupApplicable (string $ customerGroupId , array $ priceArray ): bool
219
+ {
220
+ return ($ customerGroupId === 'all ' && $ priceArray ['all_groups ' ])
221
+ || (is_numeric ($ customerGroupId ) && (int )$ priceArray ['cust_group ' ] === (int )$ customerGroupId );
222
+ }
223
+
224
+ /**
225
+ * Returns current Price Scope configuration value
226
+ *
227
+ * @return string
228
+ */
229
+ private function getPriceScopeConfig (): string
230
+ {
231
+ return (int )$ this ->config ->getValue ('catalog/price/scope ' , ScopeInterface::SCOPE_WEBSITE );
232
+ }
233
+
234
+ /**
235
+ * Returns current Website object
236
+ *
237
+ * @return WebsiteInterface
238
+ * @throws LocalizedException
239
+ */
240
+ private function getCurrentWebsite (): WebsiteInterface
241
+ {
242
+ return $ this ->storeManager ->getWebsite ();
243
+ }
206
244
}
0 commit comments