I currently have price, sku and variant title swapping when clicking between variants on a products details page.
This works with AlpineJS and looks something this. https://codepen.io/amasci/pen/PoWxbPX
<div class="flex-1 p-8" x-data="{
price: '{{- product.defaultPriceAsCurrency|trim -}}',
variant: '{{- product.defaultVariant.title|trim -}}',
sku: '{{- product.defaultSku|trim -}}'
}"
<div class="flex flex-wrap items-center pb-6 mb-12 space-x-8 border-b border-gray-200">
<div class="text-2xl text-blue-500" x-text="price"></div>
<div class="mt-1 text-sm flex items-center space-x-2">
<span class="text-blue-200" x-text="variant"></span>
<span class="mx-1 text-gray-200">•</span>
<span class="text-blue-200" x-text="sku"></span>
</div>
</div>
{% for variant in product.variants %}
<label class="relative transition cursor-pointer variant-option choice-button focus-within:ring-1 focus-within:ring-offset-2 focus-within:ring-blue-500"
for="{{ variant.id }}"
>
<input type="radio"
@click="price='{{- variant.priceAsCurrency|trim -}}', variant='{{- variant.title|trim -}}', sku='{{- variant.sku|trim -}}'"
id="{{ variant.id }}"
name="purchasables[1][id]"
value="{{ variant.id }}"
class="sr-only"
{{ loop.first ? 'checked' }}
>
<span class="block py-2 px-4 text-sm text-gray-400 border border-gray-300 transition variant-label hover:border-gray-400 hover:text-gray-500">
{{ variant.title }}
</span>
</label>
{% endfor %}
However, I have come to now add some functionality for sale prices. Whereby I would like to show the Price with a strike and the new sale price.
{% if variant.onSale %}
<del class="text-sm text-gray-500">{{ variant.priceAsCurrency }}</del>
<br>{{ variant.salePriceAsCurrency }}
{% else %}
{{ variant.priceAsCurrency }}
{% endif %}
What is the best way to adopt my code to do this? I cannot pass in the html code to alpine.
Should I just loop the variants and output the code and then show/hide the right block based on which radio is selected or is there a better approach?