💫Variant Integration
It's as easy as walking! The Variant Integration Feature allows you to dynamically switch product variants by sending predefined trigger values to the embedded iFrame using postMessage.
Last updated
It's as easy as walking! The Variant Integration Feature allows you to dynamically switch product variants by sending predefined trigger values to the embedded iFrame using postMessage.
Last updated
<head>
ElementTo enable the Mazing Viewer, include the following script in the <head>
element of your HTML. This script initializes the library and prepares your website for the 3D experience.
Note: The defer
attribute ensures the script is loaded after the HTML document is parsed, optimizing page performance.
<body>
Create a new script at the end of the <body>
element. This script contains the logic to load and customize your 3D experience.
Why at the End? Adding your script at the end ensures that the HTML content is fully loaded before your logic is executed, preventing errors.
In this step, you’ll add the main logic to initialize the Mazing Viewer. Copy and paste the following code into your script file or the <script>
element you created earlier (for non-Shopify users).
What’s Happening?
The waitForMazingFunction
ensures that the mazingProjectAvailable
function is fully loaded before you execute your custom script.
Once everything is ready, the script logs Mazing configuration is active :)
to confirm successful initialization.
Tip: Open the browser’s developer console to verify if the message appears. If it does, the script is working correctly.
Next, you’ll use mazingProjectAvailable()
to retrieve the iframeSrc
for the Mazing Viewer. This function fetches the SKU (Stock Keeping Unit) of the current product and initializes the viewer.
Retrieve the SKU:
The getSKU
function extracts the SKU from the product URL using a regular expression.
For example, a URL like https://example.com/products/your-product
will return your-product
as the SKU.
Generate the iFrame Source:
The mazingProjectAvailable
function uses the SKU to create the iframeSrc
, which is needed to display the Mazing Viewer.
Use the iFrame Source:
After generating the iframeSrc
, you can use it to embed the viewer on your website.
Once you have the iframeSrc
, customize and append it to your website as needed. See the Script Integration Documentation for more details.
iframeSrc
Use the iframeSrc
to dynamically generate the iFrame and embed it into your website. Here's an example script that shows how you can achieve this:
The script dynamically creates an iframe
element, assigning it the necessary attributes and styles to embed the Mazing Viewer seamlessly into your website.
What is an <iframe>
?
An <iframe>
is an HTML element that allows you to embed another HTML document or external content (like the Mazing Viewer) within your webpage. It's like creating a "window" to display content from a different source directly on your site.
The display: none
style is applied by default to hide the iFrame, preventing any visual glitches or flickering when no product is available. This behavior ensures a smooth user experience but can be customized to fit your specific requirements.
By default, the example assumes that the iframe
will be embedded into a div
element with the ID gallery-div1
. You can replace this ID with the appropriate container ID for your setup.
If no iframeSrc
is provided, a 500 Server Error
will appear in the console.
Reason: This error occurs when no model matching the SKU is found in the Mazing World.
Impact: The error is non-critical and can be safely ignored as it does not affect the feature’s functionality.
Now it’s time to use the ID/Trigger defined for each variant. This will allow you to switch between different product variations using your own custom user interface.
For example, if you have a dropdown menu for selecting variations, you can listen for a user’s selection and send a postMessage
to the Mazing Viewer. Let’s assume your dropdown menu has the ID operations-dropdown
.
Here’s how you can implement this logic:
When an option in the dropdown is clicked, the script sends a postMessage
to the Mazing Viewer. The trigger
value in the message corresponds to the variant’s ID/Trigger, which you defined earlier during variant creation. See -> Variant Operations
For instance:
Clicking the "All-Black" option sends a trigger value of "all-black"
.
This value activates the variant with the corresponding ID/Trigger.
Model Loading and Visibility
PostMessage operations are sent only after the model is fully loaded.
The model starts loading once the <iframe>
becomes visible (i.e., when display: none
is removed).
Queued Operations
Actions triggered by postMessage
will execute once the model is fully loaded.
You can either:
Enable controls after loading is complete, or
Queue operations to run automatically after the model is ready.
Once the 3D model has finished loading, an action is sent to notify you:
window.top.postMessage({
type: 'MODEL_3D_LOADED',
value: {
model: model
},
}, '*');
Here's an example of how you can check for the model loading and set up your environment:
Creating the Variant: This step demonstrates how to define a variant, such as "all-black," within your configuration.
Sending the PostMessage:
Send a postMessage
with the trigger value 'all-black' from your shop website to switch between product variants.
Congratulations! You've successfully integrated your first configurator into your website! 🎉