ProductListing (Commerce Engine)
ProductListing (Commerce Engine)
|
|
Note
This component was introduced in version |
Example implementation
product-listing.fn.tsx
import {ProductListing as HeadlessProductListing} from '@coveo/headless/commerce';
import {useEffect, useState, FunctionComponent} from 'react';
interface ProductListingProps {
controller: HeadlessProductListing;
}
export const ProductListing: FunctionComponent<ProductListingProps> = (
props
) => {
const {controller} = props;
const [state, setState] = useState(controller.state);
useEffect(() => controller.subscribe(() => setState(controller.state)), []);
if (!state.products.length) {
return <button onClick={() => controller.refresh()}>Refresh</button>;
}
return (
<ul>
{state.products.map(({ec_name, clickUri, permanentid}) => (
<li key={permanentid}>
<a href={clickUri}>{ec_name}</a>
</li>
))}
</ul>
);
};
// usage
/**
* ```tsx
* const controller = buildProductListing(engine);
*
* <ProductListing controller={controller} />;
* ```
*/
The ProductListing controller exposes a method for retrieving product listing content in a commerce interface.
Methods
executeFirstRequest
Executes the first request if it has not been executed yet.
promoteChildToParent
Finds the specified parent product and the specified child product of that parent, and makes that child the new parent. The children and totalNumberOfChildren properties of the original parent are preserved in the new parent.
This method is useful when leveraging the product grouping feature to allow users to select nested products.
For example, if a product has children (such as color variations), you can call this method when the user selects a child to make that child the new parent product, and re-render the product as such in the storefront.
Note: In the controller state, a product that has children will always include itself as its own child so that it can be rendered as a nested product, and restored as the parent product through this method as needed.
Parameters
-
child:
Omit<BaseProduct, 'children' | 'totalNumberOfChildren'>The child product that will become the new parent.
refresh
Fetches the product listing.
subscribe
Adds a callback that’s invoked on state change.
Parameters
-
listener:
() => voidA callback that’s invoked on state change.
Returns Unsubscribe: A function to remove the listener.
breadcrumbManager
Creates a BreadcrumbManager sub-controller.
Returns BreadcrumbManager: A BreadcrumbManager sub-controller.
facetGenerator
Creates a FacetGenerator sub-controller.
Returns FacetGenerator: A FacetGenerator sub-controller.
parameterManager
Creates a ParameterManager sub-controller with the specified properties.
Parameters
-
props:
ParameterManagerProps<P>Properties for the
ParameterManagersub-controller.
Returns ParameterManager<P>: A ParameterManager sub-controller.
sort
Creates a Sort sub-controller.
Parameters
-
props:
SortPropsOptional properties for the
Sortsub-controller.
Returns Sort: A Sort sub-controller.
urlManager
Creates a UrlManager sub-controller with the specified properties.
Parameters
-
props:
UrlManagerPropsProperties for the
UrlManagersub-controller.
Returns UrlManager: A UrlManager sub-controller.
interactiveProduct
Creates an InteractiveProduct sub-controller.
Parameters
-
props:
Omit<InteractiveProductCoreProps, 'responseIdSelector'>The properties for the
InteractiveProductsub-controller.
Returns InteractiveProduct: An InteractiveProduct sub-controller.
pagination
Creates a Pagination sub-controller.
Parameters
-
props:
PaginationPropsThe optional properties for the
Paginationsub-controller.
Returns Pagination: A Pagination sub-controller.
Attributes
state
A scoped and simplified part of the headless state that is relevant to the ProductListing controller.
Properties
-
error:
CommerceAPIErrorStatusResponse | null -
isLoading:
boolean -
products:
Product[] -
responseId:
string
Initialize
buildProductListing
Creates a ProductListing controller instance.
Parameters
-
engine:
CommerceEngineThe headless commerce engine.
Returns ProductListing
Sub-controllers
FacetGenerator
Properties
-
facets:
GeneratedFacetControllersThe facet sub-controllers created by the facet generator. Array of RegularFacet, DateRangeFacet, NumericFacet, and CategoryFacet.
-
state:
string[]The ordered list of facet IDs for which sub-controllers will be created and returned when the
facetsgetter is called. -
deselectAll: functionDeselects all values in all facets.
-
subscribe: functionAdds a callback that’s invoked on state change.
Parameters
-
listener:
() ⇒ voidA callback that’s invoked on state change.
Returns
Unsubscribe: A function to remove the listener.
-
Pagination
Properties
-
state:
PaginationStateA scoped and simplified part of the headless state that is relevant to the
Paginationsub-controller. -
fetchMoreProducts: functionFetches the next page of products, and appends them to the current list of products.
-
nextPage: functionNavigates to the next page.
-
previousPage: functionNavigates to the previous page.
-
selectPage: functionNavigates to a specific page.
Parameters
-
page:
numberThe page to navigate to.
-
-
setPageSize: functionSets the page size.
Parameters
-
pageSize:
numberThe page size.
-
-
subscribe: functionAdds a callback that’s invoked on state change.
Parameters
-
listener:
() ⇒ voidA callback that’s invoked on state change.
Returns
Unsubscribe: A function to remove the listener.
-
ParameterManager<P>
Properties
-
state:
ParameterManagerState<T>The state relevant to the
ParameterManagersub-controller. -
synchronize: functionUpdates the parameters in the state with the specified parameters and fetches results. Unspecified keys are reset to their initial values.
Parameters
-
parameters:
TThe parameters to synchronize.
-
-
subscribe: functionAdds a callback that’s invoked on state change.
Parameters
-
listener:
() ⇒ voidA callback that’s invoked on state change.
Returns
Unsubscribe: A function to remove the listener.
-
Sort
Properties
-
state:
SortStateA scoped and simplified part of the headless state that is relevant to the
Sortsub-controller. -
isAvailable: functionVerifies whether the specified sort criterion is available.
Parameters
-
criterion:
SortByRelevance | SortByFieldsThe sort criterion to look for.
Returns
boolean:trueif the specified sort criterion is available;falseotherwise.
-
-
isSortedBy: functionVerifies whether the specified sort criterion is the currently active one.
Parameters
-
criterion:
SortByRelevance | SortByFieldsThe sort criterion to evaluate.
Returns
boolean:trueif the specified sort criterion is the currently active one;falseotherwise.
-
-
sortBy: functionUpdates the sort criterion and executes a new query.
Parameters
-
criterion:
SortByRelevance | SortByFieldsThe new sort criterion.
-
-
subscribe: functionAdds a callback that’s invoked on state change.
Parameters
-
listener:
() ⇒ voidA callback that’s invoked on state change.
Returns
Unsubscribe: A function to remove the listener.
-
Summary<S>
Properties
-
state:
StateA scoped and simplified part of the headless state that is relevant to the
SearchSummarysub-controller. -
subscribe: functionAdds a callback that’s invoked on state change.
Parameters
-
listener:
() ⇒ voidA callback that’s invoked on state change.
Returns
Unsubscribe: A function to remove the listener.
-
UrlManager
Properties
-
state:
UrlManagerStateThe state relevant to the
UrlManagercontroller. -
synchronize: functionUpdates the search parameters in state with those from the url and launches a search.
Parameters
-
fragment:
stringThe part of the url that contains search parameters. For example:
q=windmill&f[author]=Cervantes.
-
-
subscribe: functionAdds a callback that’s invoked on state change.
Parameters
-
listener:
() ⇒ voidA callback that’s invoked on state change.
Returns
Unsubscribe: A function to remove the listener.
-
Related types
HighlightKeyword
Properties
-
length:
numberThe length of the offset.
-
offset:
numberThe 0 based offset inside the string where the highlight should start.
InteractiveProduct
Properties
-
warningMessage?:
string -
beginDelayedSelect: functionPrepares to select the result after a certain delay, sending analytics if it was never selected before.
In a DOM context, it’s recommended to call this method on the touchstart event.
-
cancelPendingSelect: functionCancels the pending selection caused by
beginDelayedSelect.
In a DOM context, it’s recommended to call this method on the touchend event.
-
select: functionSelects the result, logging a UA event to the Coveo Platform if the result wasn’t selected before.
In a DOM context, it’s recommended to call this method on all of the following events:
-
contextmenu*click*mouseup*mousedown
PaginationProps
Properties
-
options?:
Omit<CorePaginationOptions, 'slotId'>
PaginationState
Properties
-
page:
number -
pageSize:
number -
totalEntries:
number -
totalPages:
number
ParameterManagerInitialState<T>
Properties
-
parameters:
TThe parameters affecting the response.
ParameterManagerProps<P>
Properties
-
initialState:
ParameterManagerInitialState<T>The initial state that should be applied to the
ParameterManagersub-controller.
ParameterManagerState<T>
Properties
-
parameters:
TThe parameters affecting the response.
Product
Properties
-
position:
numberThe 1-based product’s position across the non-paginated result set.
For example, if the product is the third one on the second page, and there are 10 products per page, its position is 13 (not 3).
-
additionalFields:
Record<string, unknown>The requested additional fields for the product.
-
children:
Omit<BaseProduct, 'children' | 'totalNumberOfChildren'>The child products of the product, fetched through product grouping.
-
clickUri:
stringThe URL of the product.
-
ec_brand:
string | nullThe brand of the product.
From the
ec_brandfield. -
ec_category:
string[]The category of the product (for example,
"Electronics;Electronics|Televisions;Electronics|Televisions|4K Televisions").From the
ec_categoryfield. -
ec_color:
string | nullThe color of the product.
-
ec_description:
string | nullThe description of the product.
From the
ec_descriptionfield. -
ec_gender:
string | nullThe gender the product is intended for.
-
ec_images:
string[]The URLs of additional product images.
From the
ec_imagesfield. -
ec_in_stock:
boolean | nullWhether the product is currently in stock.
From the
ec_in_stockfield. -
ec_item_group_id:
string | nullThe ID used for the purpose of product grouping.
From the
ec_item_group_idfield. -
ec_listing:
string | nullThe listing that the product belongs to.
-
ec_name:
string | nullThe name of the product.
From the
ec_namefield. -
ec_price:
number | nullThe base price of the product.
From the
ec_pricefield. -
ec_product_id:
string | nullThe product ID.
-
ec_promo_price:
number | nullThe promotional price of the product.
From the
ec_promo_pricefield. -
ec_rating:
number | nullThe product rating, from 0 to 10.
From the
ec_ratingfield. -
ec_shortdesc:
string | nullA short description of the product.
From the
ec_shortdescfield. -
ec_thumbnails:
string[]The URLs of the product image thumbnails.
From the
ec_thumbnailsfield. -
permanentid:
stringThe SKU of the product.
-
totalNumberOfChildren:
number | nullThe total number of child products fetched through product grouping.
-
excerpt?:
string | nullThe contextual excerpt generated for the product.
-
excerptsHighlights?:
HighlightKeyword[]The length and offset of each word to highlight in the product excerpt string.
-
nameHighlights?:
HighlightKeyword[]The length and offset of each word to highlight in the product name.
SortInitialState
Properties
-
criterion?:
SortByRelevance | SortByFieldsThe initial sort criterion to register in state.
SortProps
Properties
-
initialState?:
SortInitialStateThe initial state that should be applied to this
Sortsub-controller.
SortState
Properties
-
appliedSort:
SortByRelevance | SortByFieldsThe current sort criterion.
-
availableSorts:
SortByRelevance | SortByFieldsThe available sort criteria.
Unsubscribe
Call signatures
-
(): void;
UrlManagerInitialState
Properties
-
fragment:
stringThe part of the url that contains search parameters. For example:
q=windmill&f[author]=Cervantes.
UrlManagerProps
Properties
-
initialState:
UrlManagerInitialStateThe initial state that should be applied to the
UrlManagercontroller.
UrlManagerState
Properties
-
fragment:
stringThe part of the url that contains search parameters. For example:
q=windmill&f[author]=Cervantes.