Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing CartItem Variation Field in Cart Query #284

Closed
hwsiew opened this issue May 14, 2020 · 9 comments · Fixed by #383
Closed

Missing CartItem Variation Field in Cart Query #284

hwsiew opened this issue May 14, 2020 · 9 comments · Fixed by #383
Labels
awaiting author response Awaiting response from Issue/PR author

Comments

@hwsiew
Copy link
Contributor

hwsiew commented May 14, 2020

Describe the bug
There are cases where product variation's attributes can be set to 'Any' instead of particular selection as shown below. Currently, to query cart items with variable product, one can only get the attribute variation from product (e.g. ProductVariation ). This will work provided all attributes in variation product is set to specific value, for instance, the Pattern and Size attributes in the following attachment. However, if one of the attribute is set to 'Any', in this case the 'Color'. product variation for that attribute (i.e Color) will return empty string. There is no field available to query CartItem's variation for now.

image

image

image

To Reproduce
Steps to reproduce the behavior:

  1. Create variable product with 'Any' variations.
  2. Add the product variation with addToCart mutation
  3. Query the Cart
  4. Check the CartItem's Product Variation

Expected behavior
To be able to display selected attributes for CartItem like in the woocommerce cart page.

image

@kidunot89
Copy link
Member

kidunot89 commented May 14, 2020

@hwsiew If you don't have a product attribute assigned to the variation meaning set to Any it will return a empty field "". This behavior is correct.

Product Attributes without a variation are meant to be descriptive only, otherwise it should be assigned to a variation, so inventory can be kept on the item.

@kidunot89 kidunot89 added the awaiting author response Awaiting response from Issue/PR author label May 14, 2020
@hwsiew
Copy link
Contributor Author

hwsiew commented May 15, 2020

Hi @kidunot89, customer will still need to choose one for the 'Any' attribute. So how can i get the field for the cart item? In this case, e.g. the 'Color' attribute is set to 'Any', how do i get the field if customer chose 'Green'? Like the one in woocommerce cart page, the cart item selected attributes can be listed down even for the 'Any' attribute.

image

@kidunot89
Copy link
Member

@hwsiew Then you should use the color attribute on the product not the variation.

@hwsiew
Copy link
Contributor Author

hwsiew commented May 15, 2020

Hi @kidunot89, thanks for the prompt reply.

This is the variable product setting. Each variation has 3 attributes. Among these attributes, Color is set to Any in each variation. You are right, for attribute set to Any the query of the field will be empty "". I believe this is where the problem. In the case of all attributes in a variation product is set to specific value other than Any and query the variation of the product in Cart, basically can tell the customer selection for each attributes (since each attribute is set to one value). But in the case of attribute is set to Any in a variation, the variation attribute field will be "". Unless there is other field i missed to get the customer selected option for Any attribute?

image

@kidunot89
Copy link
Member

@hwsiew That is correct. You can obtaining the options for the color field by queries attributes field on the parent product.

@hwsiew
Copy link
Contributor Author

hwsiew commented Jun 4, 2020

@kidunot89, Yup, it has been set to Any and the value from parent attributes is "". How to get the selected value by customer?

@kidunot89
Copy link
Member

kidunot89 commented Jun 4, 2020

@hwsiew I said parent product, not parent attributes.
The attributes field on the Product is very different than the attributes field on ProductVariation.

I also cannot stress enough that you are using both product variations and product attributes in a way they are not designed for.

@hwsiew
Copy link
Contributor Author

hwsiew commented Jun 5, 2020

@kidunot89, I think i get your point. By the current design, they are correct. Rather than saying this is a bug, i would say an enhancement for CartItem.variation instead. Currently the type of CartItem.variation is set to ProductVariation. It is required to resolve ProductVariation Object from $source['variation_id']

'variation' => array(
'type' => 'ProductVariation',
'description' => __( 'Selected variation of the product', 'wp-graphql-woocommerce' ),
'resolve' => function( $source, array $args, AppContext $context ) {
return ! empty( $source['variation_id'] )
? Factory::resolve_crud_object( $source['variation_id'], $context )
: null;
},
),

However, according to WC, CartItem.variation is more likely a taxonomy-ish key-value pair as follow. Notice that, even though color attribute is set to Any, the customer selected value is in the cartitem data.

{
   "key":"9ab0ede7f3b012aaaccdad439979ebd9",
   "product_id":36,
   "variation_id":114,
  "variation":{
      "attribute_pattern":"Pattern 3",
      "attribute_size":"Large L",
      "attribute_pa_color":"blue"
   },
   "quantity":1,
   "data":{

   },
   "data_hash":"e8badf94e79d313381e705040724aa42",
   "line_tax_data":{
      "subtotal":[

      ],
      "total":[

      ]
   },
   "line_subtotal":30,
   "line_subtotal_tax":0,
   "line_total":30,
   "line_tax":0
}

Related portion of WC code ->
https://github.com/woocommerce/woocommerce/blob/41ff8ec073c312f0802156172cba87c6f5aecaa0/includes/wc-template-functions.php#L3538-L3570

@kidunot89
Copy link
Member

@hwsiew See this comment.

@kidunot89 kidunot89 mentioned this issue Nov 24, 2020
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting author response Awaiting response from Issue/PR author
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants