Skip to content

Commit 3c293b5

Browse files
committed
ACP2E-1339: Orders can be placed via Rest API as a guest user even when "Allow Guest Checkout" is turned off
1 parent 47c388a commit 3c293b5

6 files changed

+83
-8
lines changed

app/code/Magento/Checkout/Plugin/Api/VerifyIsGuestCheckoutEnabledBeforeAssignBillingAddress.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Checkout\Helper\Data as CheckoutHelper;
1111
use Magento\Framework\Exception\CouldNotSaveException;
1212
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\Data\AddressInterface;
1314
use Magento\Quote\Api\GuestBillingAddressManagementInterface;
1415
use Magento\Quote\Model\QuoteIdMask;
1516
use Magento\Quote\Model\QuoteIdMaskFactory;
@@ -51,12 +52,16 @@ public function __construct(
5152
*
5253
* @param GuestBillingAddressManagementInterface $subject
5354
* @param string $cartId
55+
* @param AddressInterface $address
56+
* @param bool $useForShipping
5457
* @return void
5558
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5659
*/
5760
public function beforeAssign(
5861
GuestBillingAddressManagementInterface $subject,
59-
$cartId
62+
$cartId,
63+
AddressInterface $address,
64+
$useForShipping = false
6065
): void {
6166
/** @var $quoteIdMask QuoteIdMask */
6267
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');

app/code/Magento/Checkout/Plugin/Api/VerifyIsGuestCheckoutEnabledBeforePlaceOrder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Checkout\Helper\Data as CheckoutHelper;
1111
use Magento\Framework\Exception\CouldNotSaveException;
1212
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\Data\PaymentInterface;
1314
use Magento\Quote\Api\GuestCartManagementInterface;
1415
use Magento\Quote\Model\QuoteIdMask;
1516
use Magento\Quote\Model\QuoteIdMaskFactory;
@@ -51,12 +52,14 @@ public function __construct(
5152
*
5253
* @param GuestCartManagementInterface $subject
5354
* @param string $cartId
55+
* @param PaymentInterface|null $paymentMethod
5456
* @return void
5557
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5658
*/
5759
public function beforePlaceOrder(
5860
GuestCartManagementInterface $subject,
59-
$cartId
61+
$cartId,
62+
PaymentInterface $paymentMethod = null
6063
): void {
6164
/** @var $quoteIdMask QuoteIdMask */
6265
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');

app/code/Magento/Checkout/Plugin/Api/VerifyIsGuestCheckoutEnabledBeforeSavePaymentInformation.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Checkout\Helper\Data as CheckoutHelper;
1212
use Magento\Framework\Exception\CouldNotSaveException;
1313
use Magento\Quote\Api\CartRepositoryInterface;
14+
use Magento\Quote\Api\Data\AddressInterface;
15+
use Magento\Quote\Api\Data\PaymentInterface;
1416
use Magento\Quote\Model\QuoteIdMask;
1517
use Magento\Quote\Model\QuoteIdMaskFactory;
1618

@@ -51,12 +53,18 @@ public function __construct(
5153
*
5254
* @param GuestPaymentInformationManagementInterface $subject
5355
* @param string $cartId
56+
* @param string $email
57+
* @param PaymentInterface $paymentMethod
58+
* @param AddressInterface|null $billingAddress
5459
* @return void
5560
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5661
*/
5762
public function beforeSavePaymentInformation(
5863
GuestPaymentInformationManagementInterface $subject,
59-
$cartId
64+
$cartId,
65+
$email,
66+
PaymentInterface $paymentMethod,
67+
AddressInterface $billingAddress = null
6068
): void {
6169
/** @var $quoteIdMask QuoteIdMask */
6270
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');

app/code/Magento/Checkout/Plugin/Api/VerifyIsGuestCheckoutEnabledBeforeSaveShippingInformation.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Checkout\Plugin\Api;
99

10+
use Magento\Checkout\Api\Data\ShippingInformationInterface;
1011
use Magento\Checkout\Api\GuestShippingInformationManagementInterface;
1112
use Magento\Checkout\Helper\Data as CheckoutHelper;
1213
use Magento\Framework\Exception\CouldNotSaveException;
@@ -51,12 +52,14 @@ public function __construct(
5152
*
5253
* @param GuestShippingInformationManagementInterface $subject
5354
* @param string $cartId
55+
* @param ShippingInformationInterface $addressInformation
5456
* @return void
5557
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5658
*/
5759
public function beforeSaveAddressInformation(
5860
GuestShippingInformationManagementInterface $subject,
59-
$cartId
61+
$cartId,
62+
ShippingInformationInterface $addressInformation
6063
): void {
6164
/** @var $quoteIdMask QuoteIdMask */
6265
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');

app/code/Magento/Checkout/Plugin/Api/VerifyIsGuestCheckoutEnabledBeforeSetPaymentMethod.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Checkout\Helper\Data as CheckoutHelper;
1111
use Magento\Framework\Exception\CouldNotSaveException;
1212
use Magento\Quote\Api\CartRepositoryInterface;
13+
use Magento\Quote\Api\Data\PaymentInterface;
1314
use Magento\Quote\Api\GuestPaymentMethodManagementInterface;
1415
use Magento\Quote\Model\QuoteIdMask;
1516
use Magento\Quote\Model\QuoteIdMaskFactory;
@@ -51,12 +52,14 @@ public function __construct(
5152
*
5253
* @param GuestPaymentMethodManagementInterface $subject
5354
* @param string $cartId
55+
* @param PaymentInterface $method
5456
* @return void
5557
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5658
*/
5759
public function beforeSet(
5860
GuestPaymentMethodManagementInterface $subject,
59-
$cartId
61+
$cartId,
62+
PaymentInterface $method
6063
): void {
6164
/** @var $quoteIdMask QuoteIdMask */
6265
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');

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

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,29 @@
66

77
namespace Magento\Quote\Api;
88

9+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
10+
use Magento\Checkout\Helper\Data;
11+
use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture;
12+
use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture;
13+
use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture;
14+
use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture;
15+
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture;
16+
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
17+
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
18+
use Magento\TestFramework\Fixture\Config;
19+
use Magento\TestFramework\Fixture\DataFixture;
20+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
921
use Magento\TestFramework\TestCase\WebapiAbstract;
1022

1123
class GuestCartManagementTest extends WebapiAbstract
1224
{
13-
const SERVICE_VERSION = 'V1';
14-
const SERVICE_NAME = 'quoteGuestCartManagementV1';
15-
const RESOURCE_PATH = '/V1/guest-carts/';
25+
private const SERVICE_VERSION = 'V1';
26+
private const SERVICE_NAME = 'quoteGuestCartManagementV1';
27+
private const RESOURCE_PATH = '/V1/guest-carts/';
1628

29+
/**
30+
* @var array
31+
*/
1732
protected $createdQuotes = [];
1833

1934
/**
@@ -378,4 +393,42 @@ public function testAssignCustomerByGuestUser()
378393

379394
$this->_webApiCall($serviceInfo, $requestData);
380395
}
396+
397+
#[
398+
Config(Data::XML_PATH_GUEST_CHECKOUT, 0),
399+
DataFixture(ProductFixture::class, as: 'product'),
400+
DataFixture(GuestCartFixture::class, as: 'cart'),
401+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$product.id$']),
402+
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']),
403+
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']),
404+
DataFixture(SetGuestEmailFixture::class, ['cart_id' => '$cart.id$']),
405+
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$cart.id$']),
406+
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$cart.id$']),
407+
]
408+
public function testPlaceOrderWhenGuestCheckoutIsDisabled(): void
409+
{
410+
$this->expectExceptionMessage('Sorry, guest checkout is not available.');
411+
$fixtures = DataFixtureStorageManager::getStorage();
412+
$cart = $fixtures->get('cart');
413+
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
414+
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
415+
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
416+
->create();
417+
$quoteIdMask->load($cart->getId(), 'quote_id');
418+
//Use masked cart Id
419+
$cartId = $quoteIdMask->getMaskedId();
420+
421+
$serviceInfo = [
422+
'soap' => [
423+
'service' => 'quoteGuestCartManagementV1',
424+
'operation' => 'quoteGuestCartManagementV1PlaceOrder',
425+
'serviceVersion' => 'V1',
426+
],
427+
'rest' => [
428+
'resourcePath' => '/V1/guest-carts/' . $cartId . '/order',
429+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
430+
],
431+
];
432+
$this->_webApiCall($serviceInfo, ['cartId' => $cartId]);
433+
}
381434
}

0 commit comments

Comments
 (0)