Skip to content

Commit fe9caaf

Browse files
committed
ACP2E-778: REST API: Product names in cart always use default store view values
1 parent 802068c commit fe9caaf

File tree

8 files changed

+65
-135
lines changed

8 files changed

+65
-135
lines changed

app/code/Magento/Quote/Model/Quote/Plugin/UpdateQuoteStoreId.php

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace Magento\Quote\Model\Quote\Plugin;
99

10-
use Magento\Framework\Exception\NoSuchEntityException;
1110
use Magento\Quote\Model\Quote;
12-
use Magento\Store\Model\StoreCodeInRequestPathInterface;
11+
use Magento\Store\Api\Data\StoreInterface;
1312
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\Framework\Webapi\Request;
1414

1515
/**
1616
* Updates quote store id.
@@ -23,61 +23,76 @@ class UpdateQuoteStoreId
2323
private $storeManager;
2424

2525
/**
26-
* @var StoreCodeInRequestPathInterface
26+
* @var Request
2727
*/
28-
private $storeCodeInRequestPath;
28+
private $request;
2929

3030
/**
3131
* @param StoreManagerInterface $storeManager
32-
* @param StoreCodeInRequestPathInterface $storeCodeInRequestPath
32+
* @param Request $request
3333
*/
3434
public function __construct(
3535
StoreManagerInterface $storeManager,
36-
StoreCodeInRequestPathInterface $storeCodeInRequestPath
36+
Request $request
3737
) {
3838
$this->storeManager = $storeManager;
39-
$this->storeCodeInRequestPath = $storeCodeInRequestPath;
39+
$this->request = $request;
40+
}
41+
42+
/**
43+
* Returns store based on web-api request path.
44+
*
45+
* @param string $requestPath
46+
* @return StoreInterface|null
47+
*/
48+
private function getStore(string $requestPath): ?StoreInterface
49+
{
50+
$pathParts = explode('/', trim($requestPath, '/'));
51+
$storeCode = current($pathParts);
52+
$stores = $this->storeManager->getStores(false, true);
53+
54+
return $stores[$storeCode] ?? null;
4055
}
4156

4257
/**
4358
* Update store id in requested quote by store id from request.
4459
*
60+
* @param $quote
61+
* @return Quote
62+
*/
63+
private function loadQuote($quote): Quote
64+
{
65+
$store = $this->getStore($this->request->getPathInfo());
66+
if ($store) {
67+
$quote->setStoreId($store->getId());
68+
}
69+
70+
return $quote;
71+
}
72+
73+
/**
74+
* Update store id in requested quote by store id from guest's request.
75+
*
4576
* @param Quote $subject
4677
* @param Quote $result
4778
* @return Quote
4879
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
49-
* @throws NoSuchEntityException
5080
*/
5181
public function afterLoadByIdWithoutStore(Quote $subject, Quote $result): Quote
5282
{
53-
if ($this->storeCodeInRequestPath->hasStoreCodeInRequestPath()) {
54-
$storeId = $this->storeManager->getStore()->getId();
55-
if ((int)$storeId !== $result->getStoreId()) {
56-
$result->setStoreId($storeId);
57-
}
58-
}
59-
60-
return $result;
83+
return $this->loadQuote($result);
6184
}
6285

6386
/**
64-
* Update store id in requested quote by store id from request for registered customer.
87+
* Update store id in requested quote by store id from registered customer's request.
6588
*
6689
* @param Quote $subject
6790
* @param Quote $result
6891
* @return Quote
6992
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
70-
* @throws NoSuchEntityException
7193
*/
7294
public function afterLoadByCustomer(Quote $subject, Quote $result): Quote
7395
{
74-
if ($this->storeCodeInRequestPath->hasStoreCodeInRequestPath()) {
75-
$storeId = $this->storeManager->getStore()->getId();
76-
if ((int)$storeId !== $result->getStoreId()) {
77-
$result->setStoreId($storeId);
78-
}
79-
}
80-
81-
return $result;
96+
return $this->loadQuote($result);
8297
}
8398
}

app/code/Magento/Quote/etc/webapi_rest/di.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@
1919
<type name="Magento\Quote\Model\Quote">
2020
<plugin name="updateQuoteStoreId" type="Magento\Quote\Model\Quote\Plugin\UpdateQuoteStoreId" />
2121
</type>
22-
<preference for="Magento\Store\Model\StoreCodeInRequestPathInterface" type="Magento\Store\Model\StoreCodeInRequestPath" />
2322
</config>

app/code/Magento/Quote/etc/webapi_soap/di.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,4 @@
1616
<type name="Magento\Quote\Model\Quote">
1717
<plugin name="updateQuoteStoreId" type="Magento\Quote\Model\Quote\Plugin\UpdateQuoteStoreId" />
1818
</type>
19-
<preference for="Magento\Store\Model\StoreCodeInRequestPathInterface" type="Magento\Store\Model\StoreCodeInRequestPath" />
2019
</config>

app/code/Magento/Store/Model/StoreCodeInRequestPath.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

app/code/Magento/Store/Model/StoreCodeInRequestPathInterface.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

app/code/Magento/Webapi/Controller/PathProcessor.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,36 @@
88

99
use Magento\Framework\App\ObjectManager;
1010
use Magento\Framework\Exception\NoSuchEntityException;
11-
use Magento\Framework\Locale\ResolverInterface;
12-
use Magento\Store\Model\StoreCodeInRequestPathInterface;
13-
use Magento\Store\Model\StoreManagerInterface;
1411

1512
/**
1613
* Class PathProcessor to resolve the request path
1714
*/
1815
class PathProcessor
1916
{
2017
/** Store code alias to indicate that all stores should be affected by action */
21-
public const ALL_STORE_CODE = 'all';
18+
const ALL_STORE_CODE = 'all';
2219

2320
/**
24-
* @var StoreManagerInterface
21+
* @var \Magento\Store\Model\StoreManagerInterface
2522
*/
2623
private $storeManager;
2724

2825
/**
29-
* @var ResolverInterface
26+
* @var \Magento\Framework\Locale\ResolverInterface
3027
*/
3128
private $localeResolver;
3229

3330
/**
34-
* @var StoreCodeInRequestPathInterface
35-
*/
36-
private $storeCodeInRequestPath;
37-
38-
/**
39-
* @param StoreManagerInterface $storeManager
40-
* @param ResolverInterface|null $localeResolver
41-
* @param StoreCodeInRequestPathInterface|null $storeCodeInRequestPath
31+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
32+
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
4233
*/
4334
public function __construct(
44-
StoreManagerInterface $storeManager,
45-
ResolverInterface $localeResolver = null,
46-
?StoreCodeInRequestPathInterface $storeCodeInRequestPath = null
35+
\Magento\Store\Model\StoreManagerInterface $storeManager,
36+
\Magento\Framework\Locale\ResolverInterface $localeResolver = null
4737
) {
4838
$this->storeManager = $storeManager;
4939
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(
50-
ResolverInterface::class
51-
);
52-
$this->storeCodeInRequestPath = $storeCodeInRequestPath ?: ObjectManager::getInstance()->get(
53-
StoreCodeInRequestPathInterface::class
40+
\Magento\Framework\Locale\ResolverInterface::class
5441
);
5542
}
5643

@@ -81,7 +68,6 @@ public function process($pathInfo)
8168
$storeCode = current($pathParts);
8269
$stores = $this->storeManager->getStores(false, true);
8370
if (isset($stores[$storeCode])) {
84-
$this->storeCodeInRequestPath->setStoreCodeInRequestPath(true);
8571
$this->storeManager->setCurrentStore($storeCode);
8672
$this->localeResolver->emulate($this->storeManager->getStore()->getId());
8773
$path = '/' . ($pathParts[1] ?? '');

app/code/Magento/Webapi/etc/di.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,4 @@
6666
<argument name="fileName" xsi:type="string">webapi.xml</argument>
6767
</arguments>
6868
</type>
69-
<preference for="Magento\Store\Model\StoreCodeInRequestPathInterface" type="Magento\Store\Model\StoreCodeInRequestPath" />
7069
</config>

dev/tests/api-functional/testsuite/Magento/Quote/Api/CartAddingItemsTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Quote\Api;
99

1010
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
11+
use Magento\Framework\Exception\AuthenticationException;
1112
use Magento\Framework\Webapi\Rest\Request;
1213
use Magento\Integration\Api\CustomerTokenServiceInterface;
1314
use Magento\Quote\Model\Quote;
@@ -316,14 +317,14 @@ private function getServiceInfoAddToCart(string $token): array
316317
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_multistore.php
317318
* @magentoApiDataFixture Magento/Customer/_files/customer.php
318319
*
319-
* @param string $storeCode
320320
* @param string $expectedProductName
321+
* @param string|null $storeCode
321322
*
322323
* @return void
323324
* @dataProvider dataProviderForMultiStoreView
324-
* @throws \Magento\Framework\Exception\AuthenticationException
325+
* @throws AuthenticationException
325326
*/
326-
public function testForProductNameAsPerStoreView(string $storeCode, string $expectedProductName): void
327+
public function testForProductNameAsPerStoreView(string $expectedProductName, ?string $storeCode = null): void
327328
{
328329
$this->_markTestAsRestOnly();
329330

@@ -366,13 +367,9 @@ public function testForProductNameAsPerStoreView(string $storeCode, string $expe
366367
];
367368
/** @var \Magento\Quote\Api\Data\CartInterface $cart */
368369
$cart = $this->_webApiCall($serviceInfo, [], null, $storeCode);
369-
370-
$actualProductName = '';
371370
$carts = $cart['items'];
372-
foreach ($carts as $item) {
373-
$actualProductName = $item['name'];
374-
break;
375-
}
371+
$actualProductName = $carts[0]['name'] ?? '';
372+
376373
$this->assertEquals($expectedProductName, $actualProductName);
377374
}
378375

@@ -382,14 +379,18 @@ public function testForProductNameAsPerStoreView(string $storeCode, string $expe
382379
public function dataProviderForMultiStoreView(): array
383380
{
384381
return [
385-
'defaultStoreView' => [
386-
'default',
387-
'Simple Product One'
382+
'noStoreCodeInRequestPath' => [
383+
'Simple Product One',
384+
null
388385
],
389-
'secondStoreView' => [
390-
'fixturestore',
391-
'StoreTitle'
386+
'defaultStoreCodeInRequestPath' => [
387+
'Simple Product One',
388+
'default'
392389
],
390+
'secondStoreCodeInRequestPath' => [
391+
'StoreTitle',
392+
'fixturestore'
393+
]
393394
];
394395
}
395396
}

0 commit comments

Comments
 (0)