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

#141 add simple product to cart #170

Merged
merged 22 commits into from
Oct 3, 2018

Conversation

roma-glushko
Copy link
Member

@roma-glushko roma-glushko commented Sep 4, 2018

Description

The pull enables adding simple products to customer/guest carts via GraphQl mutations.

In order to add simple product to cart the following information can be passed:

  • product SKUs (required)
  • quantities (required)
  • custom options (required if product has required options)

As a result of executing such mutation, the current state of the cart will be returned. Cart items information can vary and depends on product types. For this case, simple cart item contains information about custom options.

Fixed Issues

  1. Date of birth - year only goes back to 2002 magento2#141: Add simple product to cart

Manual testing scenarios

The following query can be executed to test the changes:

mutation {
  addSimpleProductsToCart(
    input: {
      cart_id: "b96566a020b8797b7d806a948174275e", 
      cartItems: [
        {
          data: {
            sku: "product_dynamic_24494", 
            qty: 1
          }, 
          customizable_options: [
            {id: 5, value: "test"},
            {id: 6, value: "5"}
          ]
        }
      ]
    }
  ) {
    
    cart {
      items {
        id
        qty
        ... on SimpleCartItem {
          customizable_options {
            id
            label
            type
            is_required
            values {
              id
              label
              value
              price {
                type
                units
                value
              }
            }
          }
        }
        product {
          sku
          name
          image
          url_key
          categories {
            id
            name
          } 
          updated_at
          websites {
            name
          }
        }
      }
    }    
  }
}

@magento-cicd2
Copy link
Contributor

magento-cicd2 commented Sep 4, 2018

CLA assistant check
All committers have signed the CLA.

@@ -1,4 +1,4 @@
# CatalogGraphQl
# QuoteGraphQl
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 accidental change

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed. Will fix that. Thank you for noticing!

/**
* Catalog product option date validator
*/
class DateType extends ProductDateOptionType
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this one needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason why it's needed is ProductDateOptionType wants to see date value as an associate array (like ['year' => '2018', 'month' => '1', 'day' => '1']). By this changes, we allow to use string as a date value. This meets our schema shape.
The same approach is used in REST API.

$optionValue = $option->getValueById($itemOption->getValue());
$selectedOptionValueData['price'] = [
'type' => strtoupper($optionValue->getPriceType()),
'units' => '$',
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this part, including files processing will be completed before we merge.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I have been implemented this mock. Thank you for noticing!

{
if (!isset($data['product'])) {
throw new GraphQlInputException(
__('Missing key %1 in cart data', ['product'])
Copy link
Contributor

@paliarush paliarush Sep 5, 2018

Choose a reason for hiding this comment

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

I don't think this error will be useful for the end user. This should probably be a regular \LogicException. Same for the other exception below.

What do you think?

}

type SelectedCustomizableOptionValue {
id: Int
Copy link
Contributor

@paliarush paliarush Sep 5, 2018

Choose a reason for hiding this comment

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

Why ID can be null?

Copy link
Member Author

Choose a reason for hiding this comment

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

It should not be. Changing to id: Int!. Thank you for mentioning!

$errorMessages[] = $error->getText();
}

return implode(PHP_EOL, $errorMessages);
Copy link
Contributor

Choose a reason for hiding this comment

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

It is probably better to use semicolon or another separator for the messages in GraphQL output.

@@ -0,0 +1,26 @@
{
"name": "magento/module-simple-product-graph-ql",
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be part of CatalogGraphql module since simple product type is defined in Catalog module.

QUERY;
$response = $this->graphQlQuery($query);

var_dump($response);
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not have var_dump in tests.


var_dump($response);

self::assertArrayHasKey('addSimpleProductsToCart', $response);
Copy link
Contributor

Choose a reason for hiding this comment

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

Would be perfect to validate all the requested fields.

Copy link
Contributor

@paliarush paliarush left a comment

Choose a reason for hiding this comment

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

Please address review comments above.

@roma-glushko roma-glushko force-pushed the 141-add-simple-product-to-cart branch from 36f075c to 50a928c Compare September 7, 2018 12:35
@roma-glushko roma-glushko changed the base branch from 2.3-develop to GraphQL-165 September 7, 2018 12:46
@naydav naydav changed the base branch from GraphQL-165 to 2.3-develop September 13, 2018 10:36
@magento-engcom-team magento-engcom-team added this to the Release: 2.3.0 milestone Sep 13, 2018
@naydav
Copy link
Contributor

naydav commented Sep 13, 2018

@roma-glushko
Pls, merge mainline and re-run tests

I found a few problems


FILE: ...pp/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php
----------------------------------------------------------------------
FOUND 6 ERRORS AFFECTING 6 LINES
----------------------------------------------------------------------
 19 | ERROR | [x] {@inheritdoc} imports only short description,
    |       |     annotation must have long description
 21 | ERROR | [x] {@inheritdoc} does not import parameter annotation
 30 | ERROR | [x] Missing short description
 31 | ERROR | [x] There must be exactly one blank line before tags
 53 | ERROR | [x] Missing short description
 54 | ERROR | [x] There must be exactly one blank line before tags
----------------------------------------------------------------------
PHPCBF CAN FIX THE 6 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


FILE: ...ml/app/code/Magento/QuoteGraphQl/Model/Hydrator/CartHydrator.php
----------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
 15 | ERROR | [x] {@inheritdoc} imports only short description,
    |       |     annotation must have long description
 19 | ERROR | [x] Missing short description
 20 | ERROR | [x] There must be exactly one blank line before tags
----------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


FILE: ...nto/QuoteGraphQl/Model/Resolver/Cart/AddSimpleProductsToCart.php
----------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
  32 | ERROR | [x] {@inheritdoc} imports only short description,
     |       |     annotation must have long description
 115 | ERROR | [x] {@inheritdoc} imports only short description,
     |       |     annotation must have long description
 117 | ERROR | [x] {@inheritdoc} does not import parameter annotation
----------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


FILE: ...nto/QuoteGraphQl/Model/Resolver/CartItem/CustomizableOptions.php
----------------------------------------------------------------------
FOUND 8 ERRORS AFFECTING 8 LINES
----------------------------------------------------------------------
  29 | ERROR | [x] {@inheritdoc} imports only short description,
     |       |     annotation must have long description
  64 | ERROR | [x] {@inheritdoc} imports only short description,
     |       |     annotation must have long description
  66 | ERROR | [x] {@inheritdoc} does not import parameter annotation
 104 | ERROR | [x] Missing short description
 105 | ERROR | [x] There must be exactly one blank line before tags
 209 | ERROR | [x] Missing short description
 210 | ERROR | [x] There must be exactly one blank line before tags
 225 | ERROR | [x] There must be exactly one blank line before tags
----------------------------------------------------------------------
PHPCBF CAN FIX THE 8 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


FILE: ...ode/Magento/QuoteGraphQl/Model/Resolver/CartItemTypeResolver.php
----------------------------------------------------------------------
FOUND 5 ERRORS AFFECTING 4 LINES
----------------------------------------------------------------------
 14 | ERROR | [x] {@inheritdoc} imports only short description,
    |       |     annotation must have long description
 32 | ERROR | [x] {@inheritdoc} imports only short description,
    |       |     annotation must have long description
 33 | ERROR | [x] There must be exactly one blank line before tags
 33 | ERROR | [x] There must be exactly one blank line between lines
 35 | ERROR | [x] {@inheritdoc} does not import parameter annotation
----------------------------------------------------------------------
PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------


FILE: ...to/QuoteGraphQl/Model/Resolver/CartItemTypeResolverComposite.php
----------------------------------------------------------------------
FOUND 5 ERRORS AFFECTING 4 LINES
----------------------------------------------------------------------
 14 | ERROR | [x] {@inheritdoc} imports only short description,
    |       |     annotation must have long description
 32 | ERROR | [x] {@inheritdoc} imports only short description,
    |       |     annotation must have long description
 33 | ERROR | [x] There must be exactly one blank line before tags
 33 | ERROR | [x] There must be exactly one blank line between lines
 35 | ERROR | [x] {@inheritdoc} does not import parameter annotation
----------------------------------------------------------------------
PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
/var/www/html/app/code/Magento/QuoteGraphQl/Model/Resolver/Cart/AddSimpleProductsToCart.php:34	The class AddSimpleProductsToCart has a coupling between objects value of 16. Consider to reduce the number of dependencies under 13.
/var/www/html/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItem/CustomizableOptions.php:111	The method getOptionData() has a Cyclomatic Complexity of 15. The configured cyclomatic complexity threshold is 10.
/var/www/html/app/code/Magento/QuoteGraphQl/Model/Resolver/CartItem/CustomizableOptions.php:111	The method getOptionData() has an NPath complexity of 720. The configured NPath complexity threshold is 200.

@naydav naydav force-pushed the 141-add-simple-product-to-cart branch from 3f3556d to 7990b83 Compare September 26, 2018 16:10
@magento-engcom-team magento-engcom-team modified the milestones: Release: 2.3.0, Release: 2.3.1 Sep 26, 2018
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.

7 participants