Skip to content

Instantly share code, notes, and snippets.

@akshuvo
Created September 5, 2025 00:38
Show Gist options
  • Save akshuvo/206cf88e902e2135141f165bc15fe126 to your computer and use it in GitHub Desktop.
Save akshuvo/206cf88e902e2135141f165bc15fe126 to your computer and use it in GitHub Desktop.
Shopify: Reloads the page with the default variant if no variant is selected
{%- comment -%}
Reloads the page with the default variant if no variant is selected
- product: Product object
use: {% render 'reload-with-default-variant', product: product %}
{%- endcomment -%}
{%- liquid
# Reload and select 12 ounce variant if no variant is selected
assign on_option_value = '12 ounce'
assign reload_with_selected_variant_id = 0
if product.selected_variant == null
for value in product.options_by_name['Weight'].values
if value.name == on_option_value
assign reload_with_selected_variant_id = value.variant.id
break
endif
endfor
endif
-%}
<script type="text/javascript">
const selectedVariantId = {{ reload_with_selected_variant_id }};
if (selectedVariantId) {
const url = new URL(window.location.href);
url.searchParams.set('variant', selectedVariantId);
window.location.href = url.href;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment