Skip to content
This repository has been archived by the owner on Dec 19, 2019. 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. [Checkout] Add grouped product to Cart #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 February 13, 2019 16:16
{
/**
* @param CartInterface $cart
* @param ContextInterface $context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomashKhamlai yes, good spot. Thanks.

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomashKhamlai makes sense. Thanks.

@TomashKhamlai
Copy link
Contributor

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

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

@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 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All functionality should be moved to GroupedProductGraphQl

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can use array_column native function

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a duplicate of a simple product handler

/**
* @inheritdoc
*/
class AddToCartHandlerResolver
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like arrayManager is overhead for this operation

Copy link
Contributor

@lenaorobei lenaorobei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ldusan84
Copy link
Contributor Author


input AddGroupedProductsToCartInput {
cart_id: String!
cartItems: [GroupedProductCartItemInput!]!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {
Copy link
Contributor

@lenaorobei lenaorobei Oct 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input GroupedProductCartItemInput {
    data: CartItemInput!
}

@lenaorobei
Copy link
Contributor

@ldusan84 please see comments. Thanks.

@ldusan84
Copy link
Contributor Author

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

}

input GroupedProductCartItemInput {
data: [CartItemInput!]!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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
@ghost
Copy link

ghost 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
Development

Successfully merging this pull request may close these issues.

5 participants