-
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.
ENGCOM-4221: Add configurable product to Cart #224
- Loading branch information
Showing
6 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/AddConfigurableProductsToCart.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,89 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\ConfigurableProductGraphQl\Model\Resolver; | ||
|
||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\Framework\Stdlib\ArrayManager; | ||
use Magento\QuoteGraphQl\Model\Cart\AddProductsToCart; | ||
use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromCart; | ||
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; | ||
|
||
/** | ||
* Add configurable products to cart GraphQl resolver | ||
* {@inheritdoc} | ||
*/ | ||
class AddConfigurableProductsToCart implements ResolverInterface | ||
{ | ||
/** | ||
* @var ArrayManager | ||
*/ | ||
private $arrayManager; | ||
|
||
/** | ||
* @var GetCartForUser | ||
*/ | ||
private $getCartForUser; | ||
|
||
/** | ||
* @var AddProductsToCart | ||
*/ | ||
private $addProductsToCart; | ||
|
||
/** | ||
* @var ExtractDataFromCart | ||
*/ | ||
private $extractDataFromCart; | ||
|
||
/** | ||
* @param ArrayManager $arrayManager | ||
* @param GetCartForUser $getCartForUser | ||
* @param AddProductsToCart $addProductsToCart | ||
* @param ExtractDataFromCart $extractDataFromCart | ||
*/ | ||
public function __construct( | ||
ArrayManager $arrayManager, | ||
GetCartForUser $getCartForUser, | ||
AddProductsToCart $addProductsToCart, | ||
ExtractDataFromCart $extractDataFromCart | ||
) { | ||
$this->arrayManager = $arrayManager; | ||
$this->getCartForUser = $getCartForUser; | ||
$this->addProductsToCart = $addProductsToCart; | ||
$this->extractDataFromCart = $extractDataFromCart; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
$cartHash = $this->arrayManager->get('input/cart_id', $args); | ||
$cartItems = $this->arrayManager->get('input/cartItems', $args); | ||
|
||
if (!isset($cartHash)) { | ||
throw new GraphQlInputException(__('Missing key "cart_id" in cart data')); | ||
} | ||
|
||
if (!isset($cartItems) || !is_array($cartItems) || empty($cartItems)) { | ||
throw new GraphQlInputException(__('Missing key "cartItems" in cart data')); | ||
} | ||
|
||
$currentUserId = $context->getUserId(); | ||
$cart = $this->getCartForUser->execute((string)$cartHash, $currentUserId); | ||
|
||
$this->addProductsToCart->execute($cart, $cartItems); | ||
$cartData = $this->extractDataFromCart->execute($cart); | ||
|
||
return [ | ||
'cart' => $cartData, | ||
]; | ||
} | ||
} |
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