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.
Update cart items with customizable options
- Loading branch information
Showing
3 changed files
with
150 additions
and
12 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
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,98 @@ | ||
<?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; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Quote\Model\Quote; | ||
|
||
/** | ||
* Update cart item | ||
* | ||
*/ | ||
class UpdateCartItem | ||
{ | ||
/** | ||
* @var DataObjectFactory | ||
*/ | ||
private $dataObjectFactory; | ||
|
||
/** | ||
* @param DataObjectFactory $dataObjectFactory | ||
*/ | ||
public function __construct( | ||
DataObjectFactory $dataObjectFactory | ||
) { | ||
$this->dataObjectFactory = $dataObjectFactory; | ||
} | ||
|
||
/** | ||
* Update cart item | ||
* | ||
* @param Quote $cart | ||
* @param int $cartItemId | ||
* @param float $qty | ||
* @param null $customizableOptionsData | ||
* @return void | ||
* @throws GraphQlInputException | ||
*/ | ||
public function execute(Quote $cart, int $cartItemId, float $qty, array $customizableOptionsData): void | ||
{ | ||
$customizableOptions = []; | ||
foreach ($customizableOptionsData as $customizableOption) { | ||
$customizableOptions[$customizableOption['id']] = $customizableOption['value']; | ||
} | ||
|
||
try { | ||
$result = $cart->updateItem( | ||
$cartItemId, | ||
$this->createBuyRequest($qty, $customizableOptions) | ||
); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException( | ||
__( | ||
'Could not update cart item: %message', | ||
['message' => $e->getMessage()] | ||
) | ||
); | ||
} | ||
|
||
if (is_string($result)) { | ||
throw new GraphQlInputException(__( | ||
'Could not update cart item: %message', | ||
['message' => $result] | ||
)); | ||
} | ||
|
||
if ($result->getHasError()) { | ||
throw new GraphQlInputException(__( | ||
'Could not update cart item: %message', | ||
['message' => $result->getMessage()] | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* Format GraphQl input data to a shape that buy request has | ||
* | ||
* @param float $qty | ||
* @param array $customOptions | ||
* @return DataObject | ||
*/ | ||
private function createBuyRequest(float $qty, array $customOptions): DataObject | ||
{ | ||
return $this->dataObjectFactory->create([ | ||
'data' => [ | ||
'qty' => $qty, | ||
'options' => $customOptions, | ||
], | ||
]); | ||
} | ||
} |
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