Variant Change
An event that keeps an eye on PDP while clicking on variant
Description
The variantChange event triggers whenever a customer selects a different product variant on the product detail page (PDP).
It allows you to update the UI or logic dynamically — for example, changing displayed price, inventory, or product image when a new variant is chosen.
Function Signature
VajroSDK.listeners.onVariantChanged(() => {
// Do the required logic
});| Parameter | Type | Required | Description |
|---|---|---|---|
| Nil | Nil | Nil | This listener does not require any parameter. |
Example Usage
VajroSDK.listeners.onVariantChanged(() => {
console.log('Variant changed:', VajroSDK.variables.product.selectedVariant);
// show the price of the selected variant
document.getElementById("price").innerText = `$${selectedVariant.price}`;
// do any functionality
});This listener is available only on PDP page embedded blocks
Best Practices
- Update UI incrementally: Refresh only the price, stock, or image instead of re-rendering the entire PDP.
- Guard against missing fields: Always check that selectedVariant exists before accessing properties.
Caveats
- Scope limited to PDP: This event only fires on product detail pages.
- No initial trigger: The listener won’t fire for the default variant when the page first loads.
- Asynchronous updates: UI elements relying on the variant data should update inside the callback, not before it fires.
Updated about 1 month ago