Managing product images in specific contexts
In this guide, you will learn how to control the product images that will be displayed in your store sections by labeling them with specific contexts on VTEX Catalog. These contexts determine where the image is displayed and are defined as follows:
- Gallery context: Restricts the image to display only in the Product Image Gallery, allowing you to curate a set of images highlighting key features.
- SKU selector context: Restricts the image to display only in the SKU selector, providing a zoomed-in view for detailed examination.
- Generic context (default): Sets the image to display in all sections.
For example, you may want to limit the number of images displayed in the Product Details Page (PDP) gallery to highlight key product features.
Before | After |
---|---|
The product image gallery displays all five images uploaded to Catalog. | After limiting the number of images, only three product images are displayed to focus on the most important product features. |
By associating images with specific contexts, you ensure only the most relevant images are in the sections.
Before you begin
To follow the instructions in this guide, you must understand the concepts of API extensions and Section override.
Instructions
For this tutorial, we will create a custom gallery for the PDP using the label feature from VTEX Catalog.
In this example, we will create a context by labeling selected images with the term
gallery
. This gallery will only display images that have been assigned the gallery
label in Catalog.Step 1: Labeling images in Catalog
Label your product images with the label field in Catalog.
This label determines where product images should be displayed in your store.
To label your images, follow these steps:
-
Access the VTEX Admin.
-
Go to Catalog > All Products and find the product you want to edit.
-
Click the
Update
button and then clickSKU
. -
Go to the SKU you want to update and click
Edit
. -
Click the
Images
tab. -
For each SKU image you want to add to the gallery, set the Label field to gallery. These are the available labels:
Label name Description gallery
Restricts the image to display only on the Product Image Gallery. generic
Includes the image in all contexts by default. Images labeled as generic
are always displayed as part of the complete set of images.skuvariation
Assigns the image to the SKU Selector. If no image has this label, the first image of the SKU is used. -
Click
Save Label
andSave
.
Step 2: Querying images by context
Update your store
ServerProduct
and ClientProduct
fragments to retrieve images based on their assigned labels. This ensures that only relevant images for specific contexts, like the PDP, are retrieved.For more information about fragments in FastStore, see the guide Extending queries using fragments.
-
Open your store code using the code editor of your choice.
-
Go to
src/fragments/ServerProduct.ts
and add the following: -
Go to
src/fragments/ClientProduct
and add the following:
❕Thelimit: 3
argument selects only three images. Omitting this argument returns all images labeledgallery
. By default, all images are returned if no context label is found.
Step 3: Overriding the ImageGallery
component
Customize the default ImageGallery component to showcase the images retrieved in the previous step. This override allows you to control how images are displayed and interacted with on your PDP.
-
In your store code, create the
ProductDetails.tsx
file inside thesrc/components/overrides
folder. -
In the
ProductDetails.tsx
file, add the following content:_50import { SectionOverride } from "@faststore/core";_50_50import { usePDP } from "@faststore/core";_50import { Image_unstable as Image } from "@faststore/core/experimental";_50_50import { ImageGallery, ImageGalleryViewer } from "@faststore/ui";_50import { useState } from "react";_50_50const SECTION = "ProductDetails" as const;_50_50const ImageComponent = ({ url, alternateName }: {url: string, alternateName?: string}) => {_50return <Image src={url} alt={alternateName} width={68} height={68}/>_50}_50_50const override: SectionOverride = {_50section: SECTION,_50components: {_50__experimentalImageGallery: {_50Component: () => {_50const { data } = usePDP();_50const [selectedIndex, setSelectedIndex] = useState<number>(0);_50_50const currentImage = data.product.galleryImages[selectedIndex];_50_50return (_50<ImageGallery_50images={data.product.galleryImages}_50ImageComponent={ImageComponent}_50selectedImageIdx={selectedIndex}_50setSelectedImageIdx={setSelectedIndex}_50data-fs-product-details-gallery="true"_50>_50<ImageGalleryViewer>_50<Image_50sizes="(max-width: 360px) 50vw, (max-width: 768px) 90vw, 50vw"_50width={691}_50height={691 * (3 / 4)}_50loading="eager"_50src={currentImage.url}_50alt={currentImage.alternateName}_50/>_50</ImageGalleryViewer>_50</ImageGallery>_50);_50},_50},_50},_50};_50_50export { override };- The
ServerProduct
andClientProduct
fragments are accessed via theusePDP
hook. - Instead of using the images being passed to the ImageGallery component, we are using the
galleryImages
we defined.
- The
-
Open a terminal and run
yarn dev
to sync the changes made in the previous steps. -
Open a pull request to your store with these changes. Once the pull request is reviewed and approved, merge it into the
main
branch.
Considerations for SKU Selector Images
The FastStore API defines a specific name for images that are supposed to display on the SKU Selector. The first image labeled
skuvariation
in Catalog will automatically display on the SKU Selector. If no image has the skuvariation
label, the first image of the SKU will be displayed.If an image needs to display on the SKU Selector and Product Gallery, you must upload it twice and assign different labels.