I am using Foxy for my ecommerce site. I have multiple product variants on a page and they are selected by clicking. In accordance with google standards I need to meet, I’m seeking help in adding/changing a url query parameter when the variant is changed. Additionally, when a url with a product variant query parameter is loaded, it should load with the referenced product variant selected.
I’m using $('#ID').click(function() to swap values in the Foxy add-to-cart HTML embed. I’m having trouble with the necessary code to modify query parameters on the same click. More so, I don’t know where to begin as far as parsing any query parameter that may be present on page load and triggering a click to select the corresponding product variant.
I havent yet figured out how to trigger a product variant selection when a variant is specified in the query on page load, but it seems I should have most of what I need now to figure it out. If anyone has any comments/suggestions/problems with the solution above, I’d be grateful to hear them. Or, of course, a solution to this second part of the problem.
Okay I’ve figured out how to parse the url on page load and select the corresponding variant by clicking on its selector.
<script>
$(document).ready(function() {
let loadedUrl = new URL(window.location.href);
let variantSearch = loadedUrl.search;
if (variantSearch.includes('variant1')) {
$('#variant1_selector').click();
} else if (variantSearch.includes('variant2')) {
$('#variant2_selector').click();
} else {
$('#variant1_selector').click();
}
});
</script>
In this case, if there is no url query variant specified, I want variant1 to be selected. Otherwise I’d just leave the ‘else’ condition off.
I coded the entirety of this second solution myself, so even though it’s quite simple, I’d appreciate any comments about its quality/robustness and whether there’s a better way to do it.
My sources: (in addition to the sources from solution 1 above)