In this how-to guide, we will be going over how to read page URI parameters from within your Volusion blocks.
Sometimes, your blocks may need to read URI parameters from the page so that you can get data—such as the name of the particular product or category—that the shopper is viewing.
These URI parameters are also known as "pageVars" in the Element ecosystem.
This guide will show you how to set up a pageVar in your block's config so that the value will be readable in your block code.
Locate the definition of your configSchema in your block code. Add a new ElementPropTypes.readOnly property to your config and set isPrivate to true. Using isPrivate will prevent merchants using your block from seeing this setting.
Example with a property called productSlug:
import { ElementPropTypes } from '@volusion/element-proptypes'
export const configSchema = {
productSlug: {
label: 'Product Slug URI Identifier',
type: ElementPropTypes.readOnly,
isPrivate: true,
},
}
Note: If you want this value to be editable in the block's settings panel by you or your agency members, you can use ElementPropTypes.string instead.
Locate the definition of your defaultConfig in your block code. Set the value for your pageVar property to be 'pageVar:pageUrlText'.
Example:
export const defaultConfig = { productSlug: 'pageVar:pageUrlText' }
The property that you added to your configSchema will now be available for use in your block's getDataProps function. You can use it to retrieve the product data.
Example:
export const getDataProps = (utils, { productSlug }) => {
return utils.client.products
.getBySlug(productSlug)
.then(product => {
product
})
.catch(() => {})
}
Note: The second argument to getDataProps() is the block's props, which are destructured in place.
getDataProps gets called like this in your block:
getDataProps(window.ElementSdk, props)
Your block's props also contain some extra data that can be useful in combination with the pageVar data.
props.queryParams returns an object with the query params from the page URI.
For example, the URI https://myvolusionstore.com/c/shirts?page=2&sortBy=Lowest%20Price will return the following value for props.queryParams in your block code:
{
page: '2',
sortBy: 'Lowest Price'
}
export const getDataProps = (utils, props) => {
// you can use props.queryParams here (or queryParams if you destructured the props argument)
As an example, you could use query params to figure out which page of results the shopper is viewing, and return corresponding products from the Volusion API.
export const getDataProps = (utils, props) => {
const { queryParams = {} } = props
const categoryId = 'something hard coded or from another API request'
const page = Number(queryParams.page) || 1
const sortMethod = queryParams.sortBy || props.defaultSortMethod
return utils.client.products
.getByCategoryId({
categoryId,
pageSize,
page,
sortMethod,
})
.then((data = {}) => {
// transform data
})
.catch(() => {
// catch error and return a default response
})
}