Created
April 28, 2025 01:22
-
-
Save edmeehan/082e2b8dbbb182ab01914975f69feaa8 to your computer and use it in GitHub Desktop.
Shopify PDP Gallery - Limit to current variant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% liquid | |
| assign current_variant = product.selected_or_first_available_variant | |
| assign variant_index = product.variants | find_index: 'id', current_variant.id | |
| assign variant_media_index = product.media | find_index: 'id', current_variant.featured_media.id | |
| assign next_variant_media_index = product.media.size | |
| assign next_variant_index = variant_index | plus: 1 | |
| if product.variants[next_variant_index] != blank | |
| assign next_variant_featured_media_id = product.variants[next_variant_index].featured_media.id | |
| endif | |
| if next_variant_featured_media_id | |
| assign next_variant_media_index = product.media | find_index: 'id', next_variant_featured_media_id | |
| endif | |
| assign media_limit = next_variant_media_index | minus: variant_media_index | |
| %} | |
| {%- for media in product.media offset: variant_media_index limit: media_limit %} | |
| {% case media.media_type %} | |
| {% when 'image' %} | |
| {{ media | image_url: '500x500' | image_tag }} | |
| {% else %} | |
| <script>console.error('media_type does not have a view', '{{media.media_type}}');</script> | |
| {% endcase %} | |
| {% endfor -%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment