Skip to content

Commit 2bdcc11

Browse files
14086: Guest cart API ignoring cartId in url for some methods
1 parent 5a9fcab commit 2bdcc11

File tree

3 files changed

+81
-25
lines changed

3 files changed

+81
-25
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Quote\Plugin;
10+
11+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
12+
use Magento\Quote\Api\Data\CartItemInterface;
13+
use Magento\Quote\Api\GuestCartItemRepositoryInterface;
14+
15+
/**
16+
* Update cart id from request param
17+
*/
18+
class UpdateCartId
19+
{
20+
/**
21+
* @var RestRequest $request
22+
*/
23+
private $request;
24+
25+
/**
26+
* @param RestRequest $request
27+
*/
28+
public function __construct(RestRequest $request)
29+
{
30+
$this->request = $request;
31+
}
32+
33+
/**
34+
* Update id from request if param cartId exist
35+
*
36+
* @param GuestCartItemRepositoryInterface $guestCartItemRepository
37+
* @param CartItemInterface $cartItem
38+
* @return void
39+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
40+
*/
41+
public function beforeSave(
42+
GuestCartItemRepositoryInterface $guestCartItemRepository,
43+
CartItemInterface $cartItem
44+
): void {
45+
if ($cartId = $this->request->getParam('cartId')) {
46+
$cartItem->setQuoteId($cartId);
47+
}
48+
}
49+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313
<plugin name="accessControl" type="Magento\Quote\Model\QuoteRepository\Plugin\AccessChangeQuoteControl" />
1414
<plugin name="authorization" type="Magento\Quote\Model\QuoteRepository\Plugin\Authorization" />
1515
</type>
16+
<type name="Magento\Quote\Api\GuestCartItemRepositoryInterface">
17+
<plugin name="updateCartIdFromRequest" type="Magento\Quote\Plugin\UpdateCartId" />
18+
</type>
1619
</config>

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Quote\Api;
87

98
use Magento\CatalogInventory\Api\StockRegistryInterface;
109
use Magento\CatalogInventory\Model\Stock;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\ObjectManager;
1112
use Magento\TestFramework\TestCase\WebapiAbstract;
1213

14+
/**
15+
* Test for Magento\Quote\Api\GuestCartItemRepositoryInterface.
16+
*/
1317
class GuestCartItemRepositoryTest extends WebapiAbstract
1418
{
15-
const SERVICE_VERSION = 'V1';
16-
const SERVICE_NAME = 'quoteGuestCartItemRepositoryV1';
17-
const RESOURCE_PATH = '/V1/guest-carts/';
19+
public const SERVICE_NAME = 'quoteGuestCartItemRepositoryV1';
20+
private const SERVICE_VERSION = 'V1';
21+
private const RESOURCE_PATH = '/V1/guest-carts/';
1822

1923
/**
20-
* @var \Magento\TestFramework\ObjectManager
24+
* @var ObjectManager
2125
*/
22-
protected $objectManager;
26+
private $objectManager;
2327

28+
/**
29+
* @inheritdoc
30+
*/
2431
protected function setUp()
2532
{
26-
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
33+
$this->objectManager = Bootstrap::getObjectManager();
2734
}
2835

2936
/**
37+
* Test quote items
38+
*
3039
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
3140
*/
3241
public function testGetList()
@@ -112,12 +121,16 @@ public function testAddItem()
112121
];
113122

114123
$requestData = [
115-
"cartItem" => [
116-
"sku" => $productSku,
117-
"qty" => 7,
118-
"quote_id" => $cartId,
124+
'cartItem' => [
125+
'sku' => $productSku,
126+
'qty' => 7,
119127
],
120128
];
129+
130+
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
131+
$requestData['cartItem']['quote_id'] = $cartId;
132+
}
133+
121134
$this->_webApiCall($serviceInfo, $requestData);
122135
$this->assertTrue($quote->hasProductId(2));
123136
$this->assertEquals(7, $quote->getItemByProduct($product)->getQty());
@@ -205,20 +218,11 @@ public function testUpdateItem(array $stockData, string $errorMessage = null)
205218
],
206219
];
207220

208-
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
209-
$requestData = [
210-
"cartItem" => [
211-
"qty" => 5,
212-
"quote_id" => $cartId,
213-
"itemId" => $itemId,
214-
],
215-
];
216-
} else {
217-
$requestData = [
218-
"cartItem" => [
219-
"qty" => 5,
220-
"quote_id" => $cartId,
221-
],
221+
$requestData['cartItem']['qty'] = 5;
222+
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
223+
$requestData['cartItem'] += [
224+
'quote_id' => $cartId,
225+
'itemId' => $itemId,
222226
];
223227
}
224228
if ($errorMessage) {

0 commit comments

Comments
 (0)