Skip to content
This repository has been archived by the owner. It is now read-only.

GraphQl-144: [Checkout] Add grouped product to Cart #369

Conversation

@ldusan84
Copy link
Contributor

@ldusan84 ldusan84 commented Feb 13, 2019

Description (*)

Add grouped product to cart feature:

  • Added handlers for add to cart in di.xml
  • Added grouped product handler
  • Added grouped product graphql schema

Fixed Issues (if relevant)

  1. #144: Add grouped product to Cart

Manual testing scenarios (*)

  1. Create empty cart
mutation {
  createEmptyCart
}
  1. Get cart id:
{
  "data": {
    "createEmptyCart": "cl5qYfT2q0hSdF5H9cee9JGLMDziVy2b"
  }
}
  1. Add grouped product:
mutation {
  addGroupedProductsToCart(
    input: {
      cart_id: "cl5qYfT2q0hSdF5H9cee9JGLMDziVy2b", 
      cartItems: [
        {
          data: {
            sku: "24-WG085_Group",
            grouped_products: [{
              sku: "24-WG085",
              qty: 1
            }]            
          }          
        }
      ]
    }
  ) {
    cart {
      items {
        id
      }
    }
  }
}
  1. The expected result:
{
  "data": {
    "addGroupedProductsToCart": {
      "cart": {
        "items": [
          {
            "id": "1"
          }
        ]
      }
    }
  }
}

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)
@ldusan84 ldusan84 requested review from naydav and removed request for naydav Feb 13, 2019
{
/**
* @param CartInterface $cart
* @param ContextInterface $context

This comment has been minimized.

@TomashKhamlai

TomashKhamlai Feb 15, 2019
Contributor

Maybe?
/**
* @param Quote $cart
* @param array $cartItemData
* @return void
*/
I hope it helps.

This comment has been minimized.

@ldusan84

ldusan84 Feb 18, 2019
Author Contributor

@TomashKhamlai yes, good spot. Thanks.

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;

This comment has been minimized.

@TomashKhamlai

TomashKhamlai Feb 15, 2019
Contributor

Shall we keep it here?
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;

This comment has been minimized.

@ldusan84

ldusan84 Feb 18, 2019
Author Contributor

@TomashKhamlai makes sense. Thanks.

@TomashKhamlai
Copy link
Contributor

@TomashKhamlai TomashKhamlai commented Feb 21, 2019

I removed the branch locally before checking this PR again. Please check the
app/code/Magento/QuoteGraphQl/Model/Cart/AddToCartHandlerInterface::execute method, I think the problem is there. If class AddProductsToCart implements AddToCartHandlerInterface then you should use Magento\Quote\Api\Data\CartInterface; as first parameter (where you pass use Magento\Quote\Model\Quote) and PHPDoc comment should also be updated.

Try

php bin/magento setup:di:compile

for more details.

@galaoleksandr
Copy link

@galaoleksandr galaoleksandr commented Mar 25, 2019

Hello. When trying to run php/bin/magento setup:di:compile got an error: PHP Fatal error: Declaration of Magento\QuoteGraphQl\Model\Cart\AddProductsToCart::execute(Magento\Quote\Model\Quote $cart, array $cartItems): void must be compatible with Magento\QuoteGraphQl\Model\Cart\AddToCartHandlerInterface::execute(Magento\Quote\Api\Data\CartInterface $cart, array $cartItemData): void in /home/mykola/sites/magento/mag23-graphql/app/code/Magento/QuoteGraphQl/Model/Cart/AddProductsToCart.php on line 18

@ldusan84
Copy link
Contributor Author

@ldusan84 ldusan84 commented Mar 27, 2019

@galaoleksandr thanks. It's fixed now.

@naydav naydav changed the title Add grouped product to cart GraphQl-144: [Checkout] Add grouped product to Cart May 2, 2019
@naydav
Copy link
Contributor

@naydav naydav commented Jun 14, 2019

@TomashKhamlai please re-check this issue, we are going to delivery functionality in the scope of the next release (pay attention need to resolve conflicts in scheme file)
Thanks

@@ -0,0 +1,156 @@
<?php

This comment has been minimized.

@naydav

naydav Jun 21, 2019
Contributor

All functionality should be moved to GroupedProductGraphQl

*/
private function extractSubProducts(array $cartItemData): array
{
$subProducts = $cartItemData['data']['grouped_products'];

This comment has been minimized.

@naydav

naydav Jun 21, 2019
Contributor

Looks like we can use array_column native function

->addFilter('sku', array_keys($subProducts) , 'in')
->create();

$products = $this->productRepository->getList($searchCriteria)->getItems();

This comment has been minimized.

@naydav

naydav Jun 21, 2019
Contributor

We can use $procudctCollection->getData() for performance improvement
We don't need whole product object, just SKUs

/**
* Add products to cart
*/
class AddGroupedProductsToCart implements AddToCartHandlerInterface

This comment has been minimized.

@naydav

naydav Jun 21, 2019
Contributor

Looks like a duplicate of a simple product handler

/**
* @inheritdoc
*/
class AddToCartHandlerResolver

This comment has been minimized.

@naydav

naydav Jun 21, 2019
Contributor

It should be an interface with product type and cart item data (under the hood will list of handlers)

*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$cartHash = $this->arrayManager->get('input/cart_id', $args);

This comment has been minimized.

@naydav

naydav Jun 21, 2019
Contributor

Looks like arrayManager is overhead for this operation

@okorshenko okorshenko added this to Work in Progress in Pull Request Progress Jul 10, 2019
@m2-community-project m2-community-project bot moved this from Work in Progress to Changes Requested in Pull Request Progress Jul 10, 2019
Copy link
Contributor

@lenaorobei lenaorobei left a comment

@ldusan84
Copy link
Contributor Author

@ldusan84 ldusan84 commented Oct 31, 2019


input AddGroupedProductsToCartInput {
cart_id: String!
cartItems: [GroupedProductCartItemInput!]!

This comment has been minimized.

@lenaorobei

lenaorobei Oct 31, 2019
Contributor

Suggested change
cartItems: [GroupedProductCartItemInput!]!
cart_items: [GroupedProductCartItemInput!]!

I believe there is typo in proposal. Let's keep snake_case for consistency.

cartItems: [GroupedProductCartItemInput!]!
}

input GroupedProductCartItemInput {

This comment has been minimized.

@lenaorobei

lenaorobei Oct 31, 2019
Contributor

input GroupedProductCartItemInput {
    data: CartItemInput!
}
@lenaorobei
Copy link
Contributor

@lenaorobei lenaorobei commented Oct 31, 2019

@ldusan84 please see comments. Thanks.

@ldusan84
Copy link
Contributor Author

@ldusan84 ldusan84 commented Oct 31, 2019

@lenaorobei thank you for the comments, can you do another quick check please?

}

input GroupedProductCartItemInput {
data: [CartItemInput!]!

This comment has been minimized.

@lenaorobei

lenaorobei Oct 31, 2019
Contributor

I think it should be data: CartItemInput!. Please have a look at Magento/QuoteGraphQl/etc/schema.graphqls:34 for example.

This comment has been minimized.

@ldusan84

ldusan84 Oct 31, 2019
Author Contributor

Yes I see what you mean, but in simple products case we can pick only one sku. For grouped products we can pick multiple with different quantities.

This comment has been minimized.

@lenaorobei

lenaorobei Oct 31, 2019
Contributor

The idea was to add separate groped items cart_items: [GroupedProductCartItemInput!]!. They will be displayed in cart as simple products anyway.


input GroupedProductCartItemInput {
data: [CartItemInput!]!
parent_sku: String @doc(description: "Grouped product SKU.")

This comment has been minimized.

@lenaorobei

lenaorobei Oct 31, 2019
Contributor

Suggested change
parent_sku: String @doc(description: "Grouped product SKU.")

This comment has been minimized.

@ldusan84

ldusan84 Nov 16, 2019
Author Contributor

@lenaorobei not sure about this one. How do we add grouped product without knowing it's sku or id?

This comment has been minimized.

@lenaorobei

lenaorobei Nov 18, 2019
Contributor

The idea was to add as a group of simples, so no parent sku is required. In shopping cart it will be listed as multiple simple products.

This comment has been minimized.

@ldusan84

ldusan84 Nov 20, 2019
Author Contributor

I think we need to know which grouped product is being added. Same as with configurable - https://github.com/magento/graphql-ce/blob/2.3-develop/app/code/Magento/ConfigurableProductGraphQl/etc/schema.graphqls#L51

@lenaorobei
Copy link
Contributor

@lenaorobei lenaorobei commented Dec 10, 2019

Sorry to say that but we need to close this PR.

As per additional investigation and conversation with PO it was decided that adding grouped product to cart scenarios can be covered using addSimpleProductsToCart mutation mutation.

DevDocs will be updated in order to reflect this data.

@lenaorobei lenaorobei closed this Dec 10, 2019
@contribution-survey
Copy link

@contribution-survey contribution-survey bot commented Dec 10, 2019

Hi @ldusan84, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants