Skip to content

Variants and options for product - #3

Merged
loan-laux merged 4 commits into
outgrow:outgrow-reaction-commerce-providerfrom
tedraykov:outgrow-reaction-commerce-provider
May 25, 2021
Merged

loan-laux merged 4 commits into
outgrow:outgrow-reaction-commerce-providerfrom
tedraykov:outgrow-reaction-commerce-provider

Conversation

@tedraykov

Copy link
Copy Markdown

Variants and options

With this change, the product variants and options are correctly displayed on the product page.

In reaction commerce, an option is a sub-variant of a product, while in Vercel commerce it has a different meaning.
In Vercel commerce each product has a set of variants and each variant has a ubiquitous set of options that defines the variant.
To understand how RC products can correctly be normalized to VC products we can look at an example:

iPhone 12 (main product)
  |
  |
  |--- Black (variant)
  |       |
  |       |--- 64GB (sub-variant) - $699
  |       |--- 128GB (sub-variant) - $799
  |       |--- 256GB (sub-variant) - $899
  |
  |
  |--- Purple (variant)
  |       |
  |       |--- 64GB (sub-variant) - $799
  |       |--- 128GB (sub-variant) - $899
  |       |--- 256GB (sub-variant) - $999

The structure of such product in RC would be:

{
    "productId": "cmVhY3Rpb24vcHJvZHVjdDpRbVI1UVh5UDZiOEpYcHNqbQ==",
    "title": "iPhone 12",
    "variants": [
        {
            "variantId": "black-iphone"
            "attributeLabel": "Color",
            "optionTitle": "Black",
            "options": [
                {
                    "variantId": "black-iphone-64gb"    
                    "attributeLabel": "Size"
                    "optionTitle": "64GB"
                },
                {
                    "variantId": "black-iphone-128gb"  
                    "attributeLabel": "Size"
                    "optionTitle": "128GB"
                }
                ...
            ]
        },
        {
            "attributeLabel": "Color",
            "optionTitle": "Purple",
            "options": [
                {
                    "variantId": "purple-iphone-64gb"
                    "attributeLabel": "Size"
                    "optionTitle": "64GB"
                },
                {    
                    "variantId": "purple-iphone-128gb"
                    "attributeLabel": "Size"
                    "optionTitle": "128GB"
                }
                ...
            ]        
        }
    ]
    ...
}

In Vercel Commerce terms, we have a product called "iPhone 12" that have 4 variants:

  • iPhone 12 - Black 64GB

  • iPhone 12 - Black 128GB

  • iPhone 12 - Purple 64GB

  • iPhone 12 - Purple 128GB

Each variant is uniquely defined by two options - color and size(capacity)

We can normalize a RC product to a VC one by flattening each RC option into a VC variant and setting its options to the RC option "option" and the parent RC variant "option".

// RC product variant
"variantId": "black-iphone"
"attributeLabel": "Color",
"optionTitle": "Black",
"options": [
    {
        "variantId": "black-iphone-64gb"    
        "attributeLabel": "Size"
        "optionTitle": "64GB"
    }
]

// VC product variant
"variantId": "black-iphone-64gb",
"options": [
    {
        "displayName": "Color",
        "values": [{"label": "orange"}]
    },
    {
        "displayName": "Size",
        "values": [{"label": "64GB"}]
    }     
]

After we have defined the variant structure we have to set all possible options that a product can have. In order to to this, we have to itterate all variants and options of a RC product and get all unique options. Then we group them by attributeLabel

In the iPhone example the options look like:

options: Array(4)
0: {
    __typename: "MultipleChoiceOption",
    id: "cmVhY3Rpb24vY2F0YWxvZ1Byb2R1Y3RWYXJpYW50Ok52U1lwcjdDdUppRHg5d1dG",
    displayName: "Size",
    values: ["64GB", "128GB", "256"]
    }
1: {
    __typename: "MultipleChoiceOption",
    id: "cmVhY3Rpb24vY2F0YWxvZ1Byb2R1Y3RWYXJpYW50Okg4aUJrTW5xa1JTckY5NTli",
    displayName: "Color",
    values: ["Black", "Purple"]
    }
length: 2

Now, when we add an item to the cart, Vercel Commerce tries to find a variant that contains all of the selected options.

tedraykov added 2 commits May 21, 2021 06:09
When generating graphql types, the generation of optional types is
redundant because the value is wrapped in Maybe type anyway.
Removing the redundancy simplifies the type checking whenever generated
types are used.
quantity
} = <CartItem>cartItem.node

console.log('imageURLs', cartItem)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This was committed by mistake.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is cartItem.node really of type CartItem? If so, we can leave it in.

? groupProductOptionsByAttributeLabel(variants)
: [],
...rest,
variants: !!variants ? normalizeProductVariants(<CatalogProductVariant[]>variants) : [],

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We should define a common way for resolving the Maybe type because it either causes type check errors or requires redundant type castings.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agreed. This can be part of another PR though.

@loan-laux loan-laux left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good apart from the missing trailing comas in objects. Ready to merge once you take care of that!

tedraykov added 2 commits May 25, 2021 12:14
Added trailing commas object literals in normalize util.
Added null checks for product and cart item normalization methods.
Formatted the code with prettier config.
Separated the nested functions in groupProductOptionsByAttributeLabel
@loan-laux
loan-laux merged commit 7de1c37 into outgrow:outgrow-reaction-commerce-provider May 25, 2021
@loan-laux

Copy link
Copy Markdown

LGTM. Thank you very much, @tedraykov!

loan-laux pushed a commit that referenced this pull request Mar 9, 2022
* Icky 161 folder and env setup (#1)

* folder and env setup

* codegen.json headers removed

Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>

* Feature/icky 194 (vercel#5)

* folder and env setup

* codegen.json headers removed

* use-cart code flow updated

* use-cart code flow updated

* Implemented get-cart functionality

* removed unused file

* getAnonymousShopperToken function added

* normalization mapping updated

* PR points resolved

* Anonymous shopper token query added

* getAnonymousShopperToken function added

* Anonymous shopper token query added

Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>

* Icky 175 (#3)

* folder and env setup

* codegen.json headers removed

* icky-175-get-site-info

* PR comments resolved

* update category Id

Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>

* Icky-169-LogIn (vercel#4)

* Update README.md

* Initial Commit

* Commited Keys

* GraphQL Changes

* GraphQL Query

* Final Changes

* Changed login.ts

* Made changes in login.ts

* Final Changes

* Refactored login.ts

* SignUp Initial Checkin

* logout Initial

* Customer Account Initial Commit

* Logout - deleted cookie

* Reverted ReadMe and UserNav file

* Final Changes

* Resolved comments

* Resolved comments 1

* Resolved comments 2

* Resolved comments 3

* Resolved comments 4

Co-authored-by: SushantJadhav <Sushant.Jadhav@kibocommerce.com>

* ICKY-166-getProducts-and-getProduct (vercel#6)

* GetProduct Initial Commit

* Passed productCode as Slug to get-product

* Moved currencyCode in Config file

* Icky 173 (vercel#9)

* Initial commit related to getAllPages

* Initial Changes

* Making documentListName configurable

* fixing dynamic page rendering and adding typescript code

Co-authored-by: amolnadagonde <amol.nadagonde@kibocommerce.com>
Co-authored-by: kibo-sushant <sushant.jadhav@blueconchtech.com>

* Feature/icky 176 (vercel#8)

* GetProduct Initial Commit

* addItemToCart function implemneted

* Add Item to cart functionality implemented

* ICKY-166-getProducts-and-getProduct (vercel#6)

* GetProduct Initial Commit

* Passed productCode as Slug to get-product

* Moved currencyCode in Config file

* Icky 173 (vercel#9)

* Initial commit related to getAllPages

* Initial Changes

* Making documentListName configurable

* fixing dynamic page rendering and adding typescript code

Co-authored-by: amolnadagonde <amol.nadagonde@kibocommerce.com>
Co-authored-by: kibo-sushant <sushant.jadhav@blueconchtech.com>

* addItemToCart function implemneted

Conflicts resolved

* Add Item to cart functionality implemented

* booleans removed from query

* cart size option enabled

* updated addItem for with and without variants products

Co-authored-by: kibo-sushant <sushant.jadhav@blueconchtech.com>
Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>
Co-authored-by: kibo-sushant <89385472+kibo-sushant@users.noreply.github.com>
Co-authored-by: kibo-kevinwatts <85258296+kibo-kevinwatts@users.noreply.github.com>
Co-authored-by: amolnadagonde <amol.nadagonde@kibocommerce.com>

* Removed types from schema.d.ts (vercel#11)

* Final Changes (vercel#16)

* Icky 177 (vercel#13)

* addItemToCart function implemneted

Conflicts resolved

* Add Item to cart functionality implemented

* remove Item from cart implemented

* removed unused code

Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>

* Icky 178 - Update Cart Quantity implemented (vercel#14)

* update cart quantity implemented

* add item to cart bug fixed

Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>

* Icky 182 (vercel#12)

* initial commit

* useSearch hook

* remove extra spaces

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* initial commit

* useSearch hook

* remove extra spaces

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* changes in product-search-vars

* remove unwanted boolean

* Feature/icky 179 (vercel#17)

* initial commit

* useSearch hook

* remove extra spaces

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* initial commit

* useSearch hook

* remove extra spaces

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* changes in product-search-vars

* remove unwanted boolean

* initial commit

* updated Provider

* usewishlist/getwishlist

* changes in provider.ts

* initial commit

* useSearch hook

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* initial commit

* useSearch hook

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* initial commit

* usewishlist/getwishlist

* updated Provider

* changes in provider.ts

* normalize wishlistitem

* changes in get-customer-account

* remove unwanted code

* resolve empty wishlist case

* resolve pr comments

Co-authored-by: kibo-sushant <sushant.jadhav@blueconchtech.com>

* token encoding and decoding fixed (vercel#19)

Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>

* Feature/icky- 180 & 181 (vercel#20)

* initial commit

* useSearch hook

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* initial commit

* useSearch hook

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* usewishlist/getwishlist

* changes in provider.ts

* changes in get-customer-account

* initial commit

* useSearch hook

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* initial commit

* useSearch hook

* revert pages and component changes

* remove extra spacing in search.tsx

* changes in catalog/products and product-search-vars

* usewishlist/getwishlist

* changes in provider.ts

* remove unwanted code

* initial commit

* resolve pr comments

* changes in add-item

* remove wishlist fragment

* remove item from wishlist

* changes in normalize.ts

* changes in additemtowishlist mutation

* resove pr comments

* Feature/icky 291 (vercel#22)

* Kibo API authentication helper handling oauth token generation / refresh and making auth ticket available to process via next server runtime config

* Update .env template with placeholder for Kibo Auth Url

* resolve ICKY-275 (vercel#24)

* Fix/icky 276 (vercel#21)

* remove Item from cart implemented

* update cart quantity implemented

* removed unused code

* ICKY 176 and 263 implemented

* ICKY 263 removed

* PR points resolved

Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>

* ICKY-263 (vercel#23)

* ICKY-263

* resolve pr comments

* resolve pr comments for customer typescript

* docs: update kibo commerce readme with env details (vercel#26)

* resolve icky-264 (vercel#25)

* chore: remove extra field from .env.template

* chore: remove extra .vscode launch json file

* chore: cleanup test message from kibocommerce fork

* chore(docs): remove extra field from .env template and .env related docs

* chore: remove test data json file

* chore: delete yarn.lock

* refactor: remove unused fetch from kibo config, remove unused CommerceProvider

* refactor: rename queries and util modules for consistency

* chore: add checkout related api noop handlers and hooks

* chore: revert modified core files

* chore(config): add kibo production sandbox cdn to image domains config

* fix: page normalizer and query for static pages

* chore: remove commented code and unnecessary imports

* fix(module paths): switch framework alias for relative path

Co-authored-by: kibo-chandradeeptalaha <89371824+kibo-chandradeeptalaha@users.noreply.github.com>
Co-authored-by: Chandradeepta <43542673+Chandradeepta@users.noreply.github.com>
Co-authored-by: kibo-geetanshuchhabra <89399259+kibo-geetanshuchhabra@users.noreply.github.com>
Co-authored-by: kibo-sushant <89385472+kibo-sushant@users.noreply.github.com>
Co-authored-by: SushantJadhav <Sushant.Jadhav@kibocommerce.com>
Co-authored-by: amolnadagonde <amol.nadagonde@kibocommerce.com>
Co-authored-by: kibo-sushant <sushant.jadhav@blueconchtech.com>
Co-authored-by: kibo-amolnadagonde <75060520+kibo-amolnadagonde@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants