Skip to main content

Setting Default Product Variant

You can control which variant is selected by default in your product templates by implementing the getDefaultProductVariant hook in your JavaScript template object.

Implementation

Add the following hook to your JavaScript template object:

Customization Examples

You can customize the logic inside getDefaultProductVariant to select different variants based on your requirements. Here are some practical examples:

Parameters

The hook receives the following parameter:
  • product (Object): The full product object containing all product data including variants

Return Value

The hook should return the variant ID (number) of the variant that you want to be selected by default when the product page loads.
If the getDefaultProductVariant function returns null, it will have no effect on the variant selection in the store. The default variant selection behavior will remain unchanged.

Complete Example

Here’s a complete example showing how to implement the default variant selection:
This implementation will return the ID of the first available variant with stock as the default variant when customers view your product pages.