Skip to content

feat(commercetools): add product variant picker mode - #11450

Open
Nando Davis (davisnando) wants to merge 2 commits into
contentful:masterfrom
davisnando:feat/commercetools-variant-picker
Open

Nando Davis (davisnando) wants to merge 2 commits into
contentful:masterfrom
davisnando:feat/commercetools-variant-picker

Conversation

@davisnando

Copy link
Copy Markdown

Purpose

The commercetools app lets editors reference products or categories. For products it always stores the SKU of the master variant, so a specific variant of a product cannot be referenced. Many commercetools projects model sellable options (sizes, colours, configurations) as variants of one product, and editors need to pick those exact variants in Contentful.

This PR adds a variant SKU type: on fields configured with it, the picker lists product variants and stores the selected variant's SKU.

Approach

  • New SKU type variant ("Product variant") in SKU_TYPES, selectable per field in the app configuration. product stays the default, so existing installations and fields behave exactly as before.
  • src/api/variants.ts
    • fetchVariants uses the same product projection search as fetchProducts (text search, or the variants.sku filter when searching by SKU) and turns every product into one row per variant with a SKU, master variant first. A SKU search returns only the matching variant.
    • Each row stores the variant SKU. Row ids are ${productId}:${variantId}, because ecommerce-app-base uses id as the list and drag-and-drop key. The name is Product name (SKU), the image is the variant's first image (falling back to the master variant), and the additional data shows the variant's own attributes. The external link points to the product in the Merchant Center.
    • fetchVariantPreviews resolves all saved SKUs, including non-master variants, with one variants.sku query and returns one row per requested SKU. Unknown SKUs are returned with isMissing: true.
  • Pagination stays per product, because commercetools paginates product projections. In variant mode a page can therefore show more rows than the page limit. I chose this over client-side re-pagination, which would need extra requests and break the offset handling in createResolver.
  • fetchPreviews and createResolver route the variant type to the new functions.
  • The CTA (index.tsx), save button and search placeholder texts (dialog.ts) now cover variants ("Select product variants", "Save 3 variants", "Search for a product or variant..."). The "search by SKU" option is also shown for variants.
  • The helpers for product attributes and the Merchant Center link in products.ts are now exported so variants reuse them. Product behaviour is unchanged.
  • AGENTS.md documents the SKU types, the unique-id requirement and the per-product pagination.

Testing steps

  1. In apps/commercetools, run npm run build: the build succeeds.
  2. npm start and install the app in a space connected to a commercetools project that has products with several variants.
  3. In the app configuration, set a field's SKU type to Product variant.
  4. In an entry, open the picker: each variant has its own row (Product name (SKU)).
  5. Turn on "search by SKU" and search for the SKU of a non-master variant: only that variant is listed.
  6. Select a non-master variant and save: the field stores that variant's SKU and the card shows the variant's image and attributes.
  7. Reload the entry: the variant still resolves. Reorder and remove items to check drag-and-drop.
  8. Enter a SKU that does not exist in the field value: it is shown as missing.
  9. Check that fields set to Product and Category behave as before.

Breaking Changes

None. variant is opt-in per field and product remains the default. Values stored by the product type are master variant SKUs, so they also resolve when a field is switched to variant.

No dependencies were added

Dependencies and/or References

Deployment

No new installation parameters and no migration. Existing installations keep their configuration; editors opt in by setting a field's SKU type to Product variant.

Adds a `variant` SKU type. Fields configured with it list one row per product variant and store the selected variant SKU, so variants other than the master variant can be referenced. The `product` SKU type stays the default and behaves as before.
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.

1 participant