This repository was archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
Add virtual products to cart #320
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
291bc0b
Add virtual products to cart
rogyar 5c810cc
VirtualCartItem declaration added
rogyar 0604975
Merge remote-tracking branch 'origin/2.3-develop' into 145-add-virtua…
naydav 3c8d954
GraphQL-145: [Cart Operations] Add virtual product to Cart
naydav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
90 changes: 90 additions & 0 deletions
90
app/code/Magento/QuoteGraphQl/Model/Resolver/AddVirtualProductsToCart.php
This file contains hidden or 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,90 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\QuoteGraphQl\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 virtual products to cart GraphQl resolver | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
class AddVirtualProductsToCart 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -15,6 +15,7 @@ type Mutation { | |
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput | ||
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart") | ||
addSimpleProductsToCart(input: AddSimpleProductsToCartInput): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart") | ||
addVirtualProductsToCart(input: AddVirtualProductsToCartInput): AddVirtualProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddVirtualProductsToCart") | ||
|
||
} | ||
|
||
input SetShippingAddressesOnCartInput { | ||
|
@@ -167,11 +168,21 @@ input AddSimpleProductsToCartInput { | |
cartItems: [SimpleProductCartItemInput!]! | ||
} | ||
|
||
input AddVirtualProductsToCartInput { | ||
cart_id: String! | ||
cartItems: [VirtualProductCartItemInput!]! | ||
} | ||
|
||
input SimpleProductCartItemInput { | ||
data: CartItemInput! | ||
customizable_options:[CustomizableOptionInput!] | ||
} | ||
|
||
input VirtualProductCartItemInput { | ||
data: CartItemInput! | ||
customizable_options:[CustomizableOptionInput!] | ||
} | ||
|
||
input CustomizableOptionInput { | ||
id: Int! | ||
value: String! | ||
|
@@ -181,10 +192,18 @@ type AddSimpleProductsToCartOutput { | |
cart: Cart! | ||
} | ||
|
||
type AddVirtualProductsToCartOutput { | ||
cart: Cart! | ||
} | ||
|
||
type SimpleCartItem implements CartItemInterface @doc(description: "Simple Cart Item") { | ||
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions") | ||
} | ||
|
||
type VirtualCartItem implements CartItemInterface @doc(description: "Virtual Cart Item") { | ||
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions") | ||
} | ||
|
||
input CartItemInput { | ||
sku: String! | ||
qty: Float! | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are not going to use the resolver for virtual/simple products, should we add an additional check for the product type in a resolver?
Currently we can add simple product via virtual product mutation and vice versa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to have 2 different resolvers with checking of product type, but all general logic should be moved to Model in a separate class
Also, need to cover functionality with API-functional tests (including negative scenarios with a wrong product type)
Thanks