Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Add configurable product to Cart #224

Merged
merged 5 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -218,3 +218,8 @@ type CartItemSelectedOptionValuePrice {
units: String!
type: PriceTypeEnum!
}

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