-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
- Loading branch information
Showing
9 changed files
with
289 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/code/Magento/Quote/Model/Cart/AddProductsToCartError.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Quote\Model\Cart; | ||
|
||
/** | ||
* Create instances of errors on adding products to cart. Identify error code based on the message | ||
*/ | ||
class AddProductsToCartError | ||
{ | ||
/**#@+ | ||
* Error message codes | ||
*/ | ||
private const ERROR_PRODUCT_NOT_FOUND = 'PRODUCT_NOT_FOUND'; | ||
private const ERROR_INSUFFICIENT_STOCK = 'INSUFFICIENT_STOCK'; | ||
private const ERROR_NOT_SALABLE = 'NOT_SALABLE'; | ||
private const ERROR_UNDEFINED = 'UNDEFINED'; | ||
/**#@-*/ | ||
|
||
/** | ||
* List of error messages and codes. | ||
*/ | ||
private const MESSAGE_CODES = [ | ||
'Could not find a product with SKU' => self::ERROR_PRODUCT_NOT_FOUND, | ||
'The required options you selected are not available' => self::ERROR_NOT_SALABLE, | ||
'Product that you are trying to add is not available.' => self::ERROR_NOT_SALABLE, | ||
'This product is out of stock' => self::ERROR_INSUFFICIENT_STOCK, | ||
'There are no source items' => self::ERROR_NOT_SALABLE, | ||
'The fewest you may purchase is' => self::ERROR_INSUFFICIENT_STOCK, | ||
'The most you may purchase is' => self::ERROR_INSUFFICIENT_STOCK, | ||
'The requested qty is not available' => self::ERROR_INSUFFICIENT_STOCK, | ||
]; | ||
|
||
/** | ||
* Returns an error object | ||
* | ||
* @param string $message | ||
* @param int $cartItemPosition | ||
* @return Data\Error | ||
*/ | ||
public function create(string $message, int $cartItemPosition = 0): Data\Error | ||
{ | ||
return new Data\Error( | ||
$message, | ||
$this->getErrorCode($message), | ||
$cartItemPosition | ||
); | ||
} | ||
|
||
/** | ||
* Get message error code. | ||
* | ||
* @param string $message | ||
* @return string | ||
*/ | ||
private function getErrorCode(string $message): string | ||
{ | ||
foreach (self::MESSAGE_CODES as $codeMessage => $code) { | ||
if (false !== stripos($message, $codeMessage)) { | ||
return $code; | ||
} | ||
} | ||
|
||
/* If no code was matched, return the default one */ | ||
return self::ERROR_UNDEFINED; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForCheckout.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Cart; | ||
|
||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Quote\Api\CartManagementInterface; | ||
use Magento\Quote\Model\Quote; | ||
|
||
/** | ||
* Get cart | ||
*/ | ||
class GetCartForCheckout | ||
{ | ||
/** | ||
* @var CheckCartCheckoutAllowance | ||
*/ | ||
private CheckCartCheckoutAllowance $checkoutAllowance; | ||
|
||
/** | ||
* @var GetCartForUser | ||
*/ | ||
private GetCartForUser $getCartForUser; | ||
|
||
/** | ||
* @param CheckCartCheckoutAllowance $checkoutAllowance | ||
* @param GetCartForUser $getCartForUser | ||
*/ | ||
public function __construct( | ||
CheckCartCheckoutAllowance $checkoutAllowance, | ||
GetCartForUser $getCartForUser | ||
) { | ||
$this->checkoutAllowance = $checkoutAllowance; | ||
$this->getCartForUser = $getCartForUser; | ||
} | ||
|
||
/** | ||
* Gets the cart for the user validated and configured for guest checkout if applicable | ||
* | ||
* @param string $cartHash | ||
* @param int|null $customerId | ||
* @param int $storeId | ||
* @return Quote | ||
* @throws GraphQlAuthorizationException | ||
* @throws GraphQlInputException | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
public function execute(string $cartHash, ?int $customerId, int $storeId): Quote | ||
{ | ||
try { | ||
$cart = $this->getCartForUser->execute($cartHash, $customerId, $storeId); | ||
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); | ||
} | ||
$this->checkoutAllowance->execute($cart); | ||
|
||
if (null === $customerId || 0 === $customerId) { | ||
if (!$cart->getCustomerEmail()) { | ||
throw new GraphQlInputException(__("Guest email for cart is missing.")); | ||
} | ||
$cart->setCheckoutMethod(CartManagementInterface::METHOD_GUEST); | ||
} | ||
|
||
return $cart; | ||
} | ||
} |
Oops, something went wrong.