Skip to content

Commit

Permalink
ENGCOM-4221: Add configurable product to Cart #224
Browse files Browse the repository at this point in the history
 - Merge Pull Request magento/graphql-ce#224 from magento/graphql-ce:142_Add_configurable_product_to_Cart
 - Merged commits:
   1. 5e7e1da
   2. d5bece5
   3. 109a44b
   4. 583ac36
   5. 69e1fd6
  • Loading branch information
magento-engcom-team committed Feb 11, 2019
2 parents 92118f5 + 69e1fd6 commit 851dbab
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 0 deletions.
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,
];
}
}
1 change: 1 addition & 0 deletions app/code/Magento/ConfigurableProductGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"magento/module-catalog": "*",
"magento/module-configurable-product": "*",
"magento/module-catalog-graph-ql": "*",
"magento/module-quote-graph-ql": "*",
"magento/framework": "*"
},
"license": [
Expand Down
30 changes: 30 additions & 0 deletions app/code/Magento/ConfigurableProductGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.
type Mutation {
addConfigurableProductsToCart(input: AddConfigurableProductsToCartInput): AddConfigurableProductsToCartOutput @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\AddConfigurableProductsToCart")
}

type ConfigurableProduct implements ProductInterface, PhysicalProductInterface, CustomizableProductInterface @doc(description: "ConfigurableProduct defines basic features of a configurable product and its simple product variants") {
variants: [ConfigurableVariant] @doc(description: "An array of variants of products") @resolver(class: "Magento\\ConfigurableProductGraphQl\\Model\\Resolver\\ConfigurableVariant")
Expand Down Expand Up @@ -35,3 +38,30 @@ type ConfigurableProductOptionsValues @doc(description: "ConfigurableProductOpti
store_label: String @doc(description: "The label of the product on the current store")
use_default_value: Boolean @doc(description: "Indicates whether to use the default_label")
}

input AddConfigurableProductsToCartInput {
cart_id: String!
cartItems: [ConfigurableProductCartItemInput!]!
}

type AddConfigurableProductsToCartOutput {
cart: Cart!
}

input ConfigurableProductCartItemInput {
data: CartItemDetailsInput!
variant_sku: String!
customizable_options:[CustomizableOptionInput!]
}

type ConfigurableCartItem implements CartItemInterface {
customizable_options: [SelectedCustomizableOption]!
configurable_options: [SelectedConfigurableOption!]!
}

type SelectedConfigurableOption {
id: Int!
option_label: String!
value_id: Int!
value_label: String!
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function __construct(
* @param Quote $cart
* @param array $cartItems
* @throws GraphQlInputException
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
*/
public function execute(Quote $cart, array $cartItems): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function __construct(
* @return void
* @throws GraphQlNoSuchEntityException
* @throws GraphQlInputException
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function execute(Quote $cart, array $cartItemData): void
{
Expand Down
5 changes: 5 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,8 @@ type CartItemSelectedOptionValuePrice {
units: String!
type: PriceTypeEnum!
}

input CartItemDetailsInput {
sku: String!
qty: Float!
}

0 comments on commit 851dbab

Please sign in to comment.