Skip to content

Commit 95c4325

Browse files
committed
Merge remote-tracking branch 'origin/AC-15148' into spartans_pr_29082025
2 parents 921d009 + 16f016f commit 95c4325

File tree

2 files changed

+124
-7
lines changed

2 files changed

+124
-7
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/MergeCarts/CartQuantityValidator.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2021 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\QuoteGraphQl\Model\Cart\MergeCarts;
99

10-
10+
use Magento\Catalog\Model\Product;
1111
use Magento\CatalogInventory\Api\StockRegistryInterface;
12+
use Magento\CatalogInventory\Model\Stock;
1213
use Magento\Framework\Exception\CouldNotSaveException;
1314
use Magento\Framework\Exception\NoSuchEntityException;
1415
use Magento\Quote\Api\CartItemRepositoryInterface;
@@ -58,7 +59,9 @@ public function validateFinalCartQuantities(CartInterface $customerCart, CartInt
5859
$product->getId(),
5960
$product->getStore()->getWebsiteId()
6061
)->getQty();
61-
if ($stockCurrentQty < $guestCartItem->getQty() + $customerCartItem->getQty()) {
62+
63+
if (($stockCurrentQty < $guestCartItem->getQty() + $customerCartItem->getQty())
64+
&& !$this->isBackordersEnabled($product)) {
6265
try {
6366
$this->cartItemRepository->deleteById($guestCart->getId(), $guestCartItem->getItemId());
6467
$modified = true;
@@ -73,4 +76,20 @@ public function validateFinalCartQuantities(CartInterface $customerCart, CartInt
7376
}
7477
return $modified;
7578
}
79+
80+
/**
81+
* Check if backorders are enabled for the stock item
82+
*
83+
* @param Product $product
84+
* @return bool
85+
*/
86+
private function isBackordersEnabled(Product $product): bool
87+
{
88+
$backorders = $this->stockRegistry->getStockItem(
89+
$product->getId(),
90+
$product->getStore()->getWebsiteId()
91+
)->getBackorders();
92+
return $backorders == Stock::BACKORDERS_YES_NONOTIFY ||
93+
$backorders == Stock::BACKORDERS_YES_NOTIFY;
94+
}
7695
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/MergeCartsTest.php

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2019 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -12,16 +12,19 @@
1212
use Magento\Bundle\Test\Fixture\Option as BundleOptionFixture;
1313
use Magento\Bundle\Test\Fixture\Product as BundleProductFixture;
1414
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
15+
use Magento\Catalog\Test\Fixture\ProductStock as ProductStockFixture;
1516
use Magento\Customer\Test\Fixture\Customer;
17+
use Magento\Indexer\Test\Fixture\Indexer as IndexerFixture;
1618
use Magento\Quote\Model\QuoteFactory;
1719
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1820
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
21+
use Magento\Quote\Test\Fixture\AddProductToCart;
1922
use Magento\Quote\Test\Fixture\CustomerCart;
2023
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
24+
use Magento\TestFramework\Fixture\Config as ConfigFixture;
2125
use Magento\TestFramework\Fixture\DataFixture;
2226
use Magento\TestFramework\Fixture\DataFixtureStorage;
2327
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
24-
use Magento\TestFramework\Fixture\DbIsolation;
2528
use Magento\TestFramework\Helper\Bootstrap;
2629
use Magento\TestFramework\TestCase\GraphQlAbstract;
2730
use Magento\Integration\Api\CustomerTokenServiceInterface;
@@ -200,6 +203,101 @@ public function testMergeGuestWithCustomerCartBundleProduct()
200203
self::assertEquals(3, $item1['quantity']);
201204
}
202205

206+
#[
207+
ConfigFixture('cataloginventory/options/backorders', 1), // 1 = BACKORDERS_YES_NOTIFY
208+
ConfigFixture('cataloginventory/item_options/use_config_backorders', 0),
209+
DataFixture(ProductFixture::class, [
210+
'extension_attributes' => [
211+
'stock_item' => [
212+
'use_config_backorders' => false,
213+
'backorders' => 1,
214+
'is_in_stock' => 1
215+
]
216+
]
217+
], as: 'product'),
218+
DataFixture(
219+
ProductStockFixture::class,
220+
[
221+
'prod_id' => '$product.id$',
222+
'prod_qty' => 0
223+
]
224+
),
225+
DataFixture(IndexerFixture::class),
226+
DataFixture(GuestCartFixture::class, as: 'guestCart'),
227+
DataFixture(
228+
AddProductToCart::class,
229+
[
230+
'cart_id' => '$guestCart.id$',
231+
'product_id' => '$product.id$',
232+
'qty' => 3
233+
]
234+
),
235+
DataFixture(Customer::class, as: 'customer'),
236+
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], 'customerCart'),
237+
DataFixture(
238+
AddProductToCart::class,
239+
[
240+
'cart_id' => '$customerCart.id$',
241+
'product_id' => '$product.id$',
242+
'qty' => 2
243+
]
244+
),
245+
]
246+
public function testMergeGuestWithCustomerCartBackorderProduct()
247+
{
248+
$updatedQuantity = 5; //including 3 from guest cart and 2 from customer cart
249+
$guestCartId = (int)$this->fixtures->get('guestCart')->getId();
250+
$customerCartId = (int)$this->fixtures->get('customerCart')->getId();
251+
$customerEmail = $this->fixtures->get('customer')->getEmail();
252+
$productSku = $this->fixtures->get('product')->getSku();
253+
254+
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute($guestCartId);
255+
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute($customerCartId);
256+
if (!$customerQuoteMaskedId) {
257+
$quoteIdMask = $this->quoteIdMaskedFactory->create()->setQuoteId($customerCartId);
258+
$this->quoteIdMaskedResource->save($quoteIdMask);
259+
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute($customerCartId);
260+
}
261+
262+
$queryHeader = $this->getHeaderMap($customerEmail);
263+
$cartMergeQuery = $this->getCartMergeMutation($guestQuoteMaskedId, $customerQuoteMaskedId);
264+
265+
$mergeResponse = $this->graphQlMutation($cartMergeQuery, [], '', $queryHeader);
266+
$this->assertEquals(
267+
[
268+
"mergeCarts" => [
269+
"items" => [
270+
0 => [
271+
"quantity" => $updatedQuantity,
272+
"product" => [
273+
"sku" => $productSku,
274+
]
275+
]
276+
]
277+
]
278+
],
279+
$mergeResponse
280+
);
281+
282+
$cartQuery = $this->getCartQuery($customerQuoteMaskedId);
283+
$cartResponse = $this->graphQlMutation($cartQuery, [], '', $queryHeader);
284+
$this->assertEquals(
285+
[
286+
"cart" => [
287+
"items" => [
288+
0 => [
289+
"quantity" => $updatedQuantity,
290+
"product" => [
291+
"sku" => $productSku,
292+
]
293+
]
294+
]
295+
]
296+
],
297+
$cartResponse
298+
);
299+
}
300+
203301
/**
204302
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
205303
* @magentoApiDataFixture Magento/Customer/_files/customer.php

0 commit comments

Comments
 (0)