Skip to content

Commit eea979a

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-2689' into Tier4-Kings-PR-03-05-2024
2 parents f89a447 + c460877 commit eea979a

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed

app/code/Magento/Catalog/Model/Product/Price/Validation/TierPriceValidator.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
use Magento\Framework\Exception\LocalizedException;
1717
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
1818
use Magento\Store\Api\WebsiteRepositoryInterface;
19+
use Magento\Framework\App\ObjectManager;
20+
use Magento\Framework\App\Config\ScopeConfigInterface;
21+
use Magento\Catalog\Helper\Data;
22+
use Magento\Store\Model\ScopeInterface;
23+
use Magento\Framework\Exception\NoSuchEntityException;
1924

2025
/**
2126
* Validate Tier Price and check duplication
@@ -91,6 +96,11 @@ class TierPriceValidator implements ResetAfterRequestInterface
9196
*/
9297
private $productsCacheBySku = [];
9398

99+
/**
100+
* @var ScopeConfigInterface
101+
*/
102+
private $scopeConfig;
103+
94104
/**
95105
* TierPriceValidator constructor.
96106
*
@@ -103,17 +113,20 @@ class TierPriceValidator implements ResetAfterRequestInterface
103113
* @param InvalidSkuProcessor $invalidSkuProcessor
104114
* @param ProductRepositoryInterface $productRepository
105115
* @param array $allowedProductTypes [optional]
116+
* @param ScopeConfigInterface|null $scopeConfig
117+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
106118
*/
107119
public function __construct(
108-
ProductIdLocatorInterface $productIdLocator,
109-
SearchCriteriaBuilder $searchCriteriaBuilder,
110-
FilterBuilder $filterBuilder,
111-
GroupRepositoryInterface $customerGroupRepository,
112-
WebsiteRepositoryInterface $websiteRepository,
113-
Result $validationResult,
114-
InvalidSkuProcessor $invalidSkuProcessor,
120+
ProductIdLocatorInterface $productIdLocator,
121+
SearchCriteriaBuilder $searchCriteriaBuilder,
122+
FilterBuilder $filterBuilder,
123+
GroupRepositoryInterface $customerGroupRepository,
124+
WebsiteRepositoryInterface $websiteRepository,
125+
Result $validationResult,
126+
InvalidSkuProcessor $invalidSkuProcessor,
115127
ProductRepositoryInterface $productRepository,
116-
array $allowedProductTypes = []
128+
array $allowedProductTypes = [],
129+
?ScopeConfigInterface $scopeConfig = null
117130
) {
118131
$this->productIdLocator = $productIdLocator;
119132
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
@@ -124,6 +137,7 @@ public function __construct(
124137
$this->invalidSkuProcessor = $invalidSkuProcessor;
125138
$this->productRepository = $productRepository;
126139
$this->allowedProductTypes = $allowedProductTypes;
140+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
127141
}
128142

129143
/**
@@ -355,10 +369,19 @@ private function checkQuantity(TierPriceInterface $price, $key, Result $validati
355369
* @param Result $validationResult
356370
* @return void
357371
*/
358-
private function checkWebsite(TierPriceInterface $price, $key, Result $validationResult)
372+
private function checkWebsite(TierPriceInterface $price, $key, Result $validationResult): void
359373
{
360374
try {
361375
$this->websiteRepository->getById($price->getWebsiteId());
376+
$isWebsiteScope = $this->scopeConfig
377+
->isSetFlag(
378+
Data::XML_PATH_PRICE_SCOPE,
379+
ScopeInterface::SCOPE_STORE,
380+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
381+
);
382+
if (!$isWebsiteScope && (int) $this->allWebsitesValue !== $price->getWebsiteId()) {
383+
throw NoSuchEntityException::singleField('website_id', $price->getWebsiteId());
384+
}
362385
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
363386
$validationResult->addFailedItem(
364387
$key,

dev/tests/api-functional/testsuite/Magento/Catalog/Api/TierPriceStorageTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Catalog\Api\Data\TierPriceInterface;
1010
use Magento\Framework\Webapi\Rest\Request;
1111
use Magento\TestFramework\TestCase\WebapiAbstract;
12+
use Magento\TestFramework\Fixture\DataFixture;
13+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
14+
use Magento\Customer\Model\Group;
1215

1316
/**
1417
* Test all API calls for tier price storage.
@@ -300,6 +303,57 @@ public function testDelete()
300303
$this->assertEquals($pricesToStore, $tierPrice);
301304
}
302305

306+
/**
307+
* Test to validate the incorrect website id.
308+
*/
309+
#[
310+
DataFixture(
311+
ProductFixture::class,
312+
[
313+
'sku' => 'simple',
314+
'website_id' => 0,
315+
'tier_prices' => [
316+
[
317+
'customer_group_id' => Group::NOT_LOGGED_IN_ID,
318+
'qty' => 3.2,
319+
'value' => 6
320+
]
321+
]
322+
]
323+
)
324+
]
325+
public function testCheckWebsite()
326+
{
327+
$serviceInfo = [
328+
'rest' => [
329+
'resourcePath' => '/V1/products/tier-prices',
330+
'httpMethod' => Request::HTTP_METHOD_POST
331+
],
332+
'soap' => [
333+
'service' => self::SERVICE_NAME,
334+
'serviceVersion' => self::SERVICE_VERSION,
335+
'operation' => self::SERVICE_NAME . 'Update',
336+
],
337+
];
338+
$tierPriceWithInvalidWebsiteId = [
339+
'price' => 38.97,
340+
'price_type' => TierPriceInterface::PRICE_TYPE_FIXED,
341+
'website_id' => 1,
342+
'sku' => self::SIMPLE_PRODUCT_SKU,
343+
'customer_group' => 'ALL GROUPS',
344+
'quantity' => 3,
345+
'extension_attributes' => []
346+
];
347+
$response = $this->_webApiCall($serviceInfo, ['prices' => [$tierPriceWithInvalidWebsiteId]]);
348+
if (is_array($response) && count($response) > 0) {
349+
$this->assertNotEmpty($response);
350+
// phpcs:disable Generic.Files.LineLength.TooLong
351+
$message = 'Invalid attribute Website ID = %websiteId. Row ID: SKU = %SKU, Website ID: %websiteId, Customer Group: %customerGroup, Quantity: %qty.';
352+
$this->assertEquals($message, $response[0]['message']);
353+
$this->assertEquals(['simple', '1', 'ALL GROUPS', '3'], $response[0]['parameters']);
354+
}
355+
}
356+
303357
/**
304358
* Check prise exists and is correct.
305359
*

0 commit comments

Comments
 (0)