-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3870 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
- Loading branch information
Showing
19 changed files
with
957 additions
and
61 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
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
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,16 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\CustomerGraphQl\Model\Customer\UpdateCustomerData"> | ||
<arguments> | ||
<argument name="restrictedKeys" xsi:type="array"> | ||
<item name="email" xsi:type="const">Magento\Customer\Api\Data\CustomerInterface::EMAIL</item> | ||
</argument> | ||
</arguments> | ||
</type> | ||
</config> |
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
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
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
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
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
118 changes: 118 additions & 0 deletions
118
app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.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,118 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Quote\Api\CartItemRepositoryInterface; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class UpdateCartItems implements ResolverInterface | ||
{ | ||
/** | ||
* @var GetCartForUser | ||
*/ | ||
private $getCartForUser; | ||
|
||
/** | ||
* @var CartItemRepositoryInterface | ||
*/ | ||
private $cartItemRepository; | ||
|
||
/** | ||
* @param GetCartForUser $getCartForUser | ||
* @param CartItemRepositoryInterface $cartItemRepository | ||
*/ | ||
public function __construct( | ||
GetCartForUser $getCartForUser, | ||
CartItemRepositoryInterface $cartItemRepository | ||
) { | ||
$this->getCartForUser = $getCartForUser; | ||
$this->cartItemRepository = $cartItemRepository; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) { | ||
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.')); | ||
} | ||
$maskedCartId = $args['input']['cart_id']; | ||
|
||
if (!isset($args['input']['cart_items']) || empty($args['input']['cart_items']) | ||
|| !is_array($args['input']['cart_items']) | ||
) { | ||
throw new GraphQlInputException(__('Required parameter "cart_items" is missing.')); | ||
} | ||
$cartItems = $args['input']['cart_items']; | ||
|
||
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId()); | ||
|
||
try { | ||
$this->processCartItems($cart, $cartItems); | ||
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__($e->getMessage()), $e); | ||
} | ||
|
||
return [ | ||
'cart' => [ | ||
'model' => $cart, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Process cart items | ||
* | ||
* @param Quote $cart | ||
* @param array $items | ||
* @throws GraphQlInputException | ||
* @throws LocalizedException | ||
*/ | ||
private function processCartItems(Quote $cart, array $items): void | ||
{ | ||
foreach ($items as $item) { | ||
if (!isset($item['cart_item_id']) || empty($item['cart_item_id'])) { | ||
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.')); | ||
} | ||
$itemId = $item['cart_item_id']; | ||
|
||
if (!isset($item['quantity'])) { | ||
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.')); | ||
} | ||
$qty = (float)$item['quantity']; | ||
|
||
$cartItem = $cart->getItemById($itemId); | ||
if ($cartItem === false) { | ||
throw new GraphQlNoSuchEntityException( | ||
__('Could not find cart item with id: %1.', $item['cart_item_id']) | ||
); | ||
} | ||
|
||
if ($qty <= 0.0) { | ||
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId); | ||
} else { | ||
$cartItem->setQty($qty); | ||
$this->cartItemRepository->save($cartItem); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.