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

Feature/gcom 1120 | Improve defaultConfigurableOptionsSelection functionality #1991

Merged
merged 16 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-buckets-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/magento-product-configurable': minor
---

improve default selected options functionality
6 changes: 5 additions & 1 deletion examples/magento-graphcms/pages/p/[url].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ type GetPageStaticProps = GetStaticProps<LayoutNavigationProps, Props, RouteProp
function ProductPage(props: Props) {
const { products, relatedUpsells, usps, sidebarUsps, pages, defaultValues } = props

const product = mergeDeep(products, relatedUpsells)?.items?.[0]
const product = mergeDeep(
products?.items?.[0],
relatedUpsells?.items?.find((item) => item?.uid === products?.items?.[0]?.uid),
)
JoshuaS98 marked this conversation as resolved.
Show resolved Hide resolved

if (!product?.sku || !product.url_key) return null

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,28 @@ fragment ConfigurableOptions on ConfigurableProduct {
...ConfigurableOptionValue
}
}
variants {
attributes {
code
uid
label
}
product {
uid
sku
name
price_range {
minimum_price {
final_price {
value
}
}
maximum_price {
final_price {
value
}
}
}
}
}
JoshuaS98 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ fragment ConfiguredVariant on SimpleProduct @injectable {
__typename
uid
name
sku
JoshuaS98 marked this conversation as resolved.
Show resolved Hide resolved
price_range {
minimum_price {
final_price {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type BaseQuery =
| null
| undefined

let warned = false
/**
* This method writes the GetConfigurableOptionsSelection query result to the Apollo cache and sets
* the defaultValues for the `<AddProductsToCartForm defaultValues={{}}/>`.
Expand All @@ -24,41 +23,39 @@ export function defaultConfigurableOptionsSelection<Q extends BaseQuery = BaseQu
return { ...query, defaultValues: {} }

const configurable = findByTypename(query?.products?.items, 'ConfigurableProduct')
const variant = findByTypename(query?.products?.items, 'SimpleProduct')
const variants = configurable?.variants
const simple = findByTypename(query?.products?.items, 'SimpleProduct')
const simpleSku = simple?.sku
JoshuaS98 marked this conversation as resolved.
Show resolved Hide resolved

if (!configurable?.url_key || !variant)
return { ...query, products: { ...query?.products, items: [requested] } }
if (!configurable?.url_key || !simple) return { ...query, products: { items: [requested] } }
JoshuaS98 marked this conversation as resolved.
Show resolved Hide resolved

const selectedOptions: string[] = []

const options = filterNonNullableKeys(configurable.configurable_options)

const warnFor: string[] = []
options.forEach((o, index) => {
const simpleValue = variant[o.attribute_code]
if (!simpleValue) warnFor.push(o.attribute_code)

filterNonNullableKeys(o.values).forEach((v) => {
if (Buffer.from(v.uid, 'base64').toString('utf8').endsWith(`/${simpleValue}`)) {
selectedOptions[index] = v.uid
}
})
})

if (process.env.NODE_ENV !== 'production' && warnFor.length && !warned) {
warned = true
const warnStr = warnFor.join(', ')
console.warn(
`[@graphcommerce/magento-product-configurable]: The following attributes were found in the configurable options: ${warnStr}. However, they were not found in the simple product. Please add the following attributes to the simple product: ${warnStr}`,
)
}
if (warnFor.length) {
return {
...query,
products: { ...query?.products, items: [requested] },
defaultValues: {},
/**
* A new feature was implemented where a simple product displays the options of a configurable
* product. On the simple product page the options of the current product are preselected.
JoshuaS98 marked this conversation as resolved.
Show resolved Hide resolved
*
* (e.g. width: 12mm | 15mm | 25mm => 25mm is preselected).
*
* We do this by checking the variants on the configurable product. => We match the preselected
* variant by comparing the sku of the current page with the available variants. => We then check
* the attributes of the variant and set the selectedOptions accordingly. => We want to always
* return the configurable item to a simple item. If we don't do this, the productpage will
* break.
*
* https://hoproj.atlassian.net/browse/GCOM-1120
*/
variants?.forEach((v) => {
const vSku = v?.product?.sku
if (vSku === simpleSku) {
JoshuaS98 marked this conversation as resolved.
Show resolved Hide resolved
v?.attributes?.forEach((a) => {
const indexOfOption = options.findIndex((o) => o.attribute_code === a?.code)
selectedOptions[indexOfOption] = a?.uid ?? ''
})
}
}
})

if (!selectedOptions.length) return { ...query, defaultValues: {} }

Expand Down Expand Up @@ -92,9 +89,9 @@ export function defaultConfigurableOptionsSelection<Q extends BaseQuery = BaseQu
uid: configurable.uid,
configurable_product_options_selection: {
__typename: 'ConfigurableProductOptionsSelection',
media_gallery: variant.media_gallery,
media_gallery: simple.media_gallery,
variant: {
...variant,
...simple,
__typename: 'SimpleProduct',
},
options_available_for_selection: options.map(({ attribute_code }) => ({
Expand Down
Loading