forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-5349: GraphQl-622: No possibility to update customizable_optio…
…ns in updateCartItems mutation magento#626
- Loading branch information
Showing
10 changed files
with
628 additions
and
109 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
75 changes: 75 additions & 0 deletions
75
app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.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,75 @@ | ||
<?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\DataObject; | ||
use Magento\Framework\DataObjectFactory; | ||
|
||
/** | ||
* Creates buy request that can be used for working with cart items | ||
*/ | ||
class CreateBuyRequest | ||
{ | ||
/** | ||
* @var DataObjectFactory | ||
*/ | ||
private $dataObjectFactory; | ||
|
||
/** | ||
* @param DataObjectFactory $dataObjectFactory | ||
*/ | ||
public function __construct( | ||
DataObjectFactory $dataObjectFactory | ||
) { | ||
$this->dataObjectFactory = $dataObjectFactory; | ||
} | ||
|
||
/** | ||
* Returns buy request for working with cart items | ||
* | ||
* @param float $qty | ||
* @param array $customizableOptionsData | ||
* @return DataObject | ||
*/ | ||
public function execute(float $qty, array $customizableOptionsData): DataObject | ||
{ | ||
$customizableOptions = []; | ||
foreach ($customizableOptionsData as $customizableOption) { | ||
if (isset($customizableOption['value_string'])) { | ||
$customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue( | ||
$customizableOption['value_string'] | ||
); | ||
} | ||
} | ||
|
||
return $this->dataObjectFactory->create( | ||
[ | ||
'data' => [ | ||
'qty' => $qty, | ||
'options' => $customizableOptions, | ||
], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Convert custom options value | ||
* | ||
* @param string $value | ||
* @return string|array | ||
*/ | ||
private function convertCustomOptionValue(string $value) | ||
{ | ||
$value = trim($value); | ||
if (substr($value, 0, 1) === "[" && | ||
substr($value, strlen($value) - 1, 1) === "]") { | ||
return explode(',', substr($value, 1, -1)); | ||
} | ||
return $value; | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
app/code/Magento/QuoteGraphQl/Model/Cart/UpdateCartItem.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,147 @@ | ||
<?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\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Quote\Api\CartItemRepositoryInterface; | ||
use Magento\Quote\Api\CartRepositoryInterface; | ||
use Magento\Quote\Model\Quote; | ||
use Magento\Quote\Model\Quote\Item; | ||
|
||
/** | ||
* Update cart item | ||
*/ | ||
class UpdateCartItem | ||
{ | ||
/** | ||
* @var CreateBuyRequest | ||
*/ | ||
private $createBuyRequest; | ||
|
||
/** | ||
* @var CartRepositoryInterface | ||
*/ | ||
private $quoteRepository; | ||
|
||
/** | ||
* @var CartItemRepositoryInterface | ||
*/ | ||
private $cartItemRepository; | ||
|
||
/** | ||
* @param CartItemRepositoryInterface $cartItemRepository | ||
* @param CartRepositoryInterface $quoteRepository | ||
* @param CreateBuyRequest $createBuyRequest | ||
*/ | ||
public function __construct( | ||
CartItemRepositoryInterface $cartItemRepository, | ||
CartRepositoryInterface $quoteRepository, | ||
CreateBuyRequest $createBuyRequest | ||
) { | ||
$this->cartItemRepository = $cartItemRepository; | ||
$this->quoteRepository = $quoteRepository; | ||
$this->createBuyRequest = $createBuyRequest; | ||
} | ||
|
||
/** | ||
* Update cart item | ||
* | ||
* @param Quote $cart | ||
* @param int $cartItemId | ||
* @param float $quantity | ||
* @param array $customizableOptionsData | ||
* @return void | ||
* @throws GraphQlInputException | ||
* @throws GraphQlNoSuchEntityException | ||
* @throws NoSuchEntityException | ||
*/ | ||
public function execute(Quote $cart, int $cartItemId, float $quantity, array $customizableOptionsData): void | ||
{ | ||
if (count($customizableOptionsData) === 0) { // Update only item's qty | ||
$this->updateItemQuantity($cartItemId, $cart, $quantity); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
$result = $cart->updateItem( | ||
$cartItemId, | ||
$this->createBuyRequest->execute($quantity, $customizableOptionsData) | ||
); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException( | ||
__( | ||
'Could not update cart item: %message', | ||
['message' => $e->getMessage()] | ||
) | ||
); | ||
} | ||
|
||
if ($result->getHasError()) { | ||
throw new GraphQlInputException( | ||
__( | ||
'Could not update cart item: %message', | ||
['message' => $result->getMessage()] | ||
) | ||
); | ||
} | ||
|
||
$this->quoteRepository->save($cart); | ||
} | ||
|
||
/** | ||
* Updates item qty for the specified cart | ||
* | ||
* @param int $itemId | ||
* @param Quote $cart | ||
* @param float $quantity | ||
* @throws GraphQlNoSuchEntityException | ||
* @throws NoSuchEntityException | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
private function updateItemQuantity(int $itemId, Quote $cart, float $quantity) | ||
{ | ||
$cartItem = $cart->getItemById($itemId); | ||
if ($cartItem === false) { | ||
throw new GraphQlNoSuchEntityException( | ||
__('Could not find cart item with id: %1.', $itemId) | ||
); | ||
} | ||
$cartItem->setQty($quantity); | ||
$this->validateCartItem($cartItem); | ||
$this->cartItemRepository->save($cartItem); | ||
} | ||
|
||
/** | ||
* Validate cart item | ||
* | ||
* @param Item $cartItem | ||
* @return void | ||
* @throws GraphQlInputException | ||
*/ | ||
private function validateCartItem(Item $cartItem): void | ||
{ | ||
if ($cartItem->getHasError()) { | ||
$errors = []; | ||
foreach ($cartItem->getMessage(false) as $message) { | ||
$errors[] = $message; | ||
} | ||
if (!empty($errors)) { | ||
throw new GraphQlInputException( | ||
__( | ||
'Could not update the product with SKU %sku: %message', | ||
['sku' => $cartItem->getSku(), 'message' => __(implode("\n", $errors))] | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.