Hey @JohnWalter ,
To implement this feature on the Dawn theme for Shopify, where each variant option displays its corresponding price, you’ll need to modify the Liquid template and add some JavaScript. Here’s a step-by-step guide to help you display the price next to each variant option.
Step 1. Update the Product Template:
-
Open the main-product.liquid file in your Dawn theme.
-
Locate the area where the variant options are displayed, usually inside product-form or product-variant sections.
-
Modify the code to include a placeholder for each variant’s price. Add a or similar element next to each option button, where the price will be displayed.
Here’s an example structure for the variant options:
{% for option in product.options_with_values %}
{% if option.name == 'Size' %}
{% for value in option.values %}
{% endfor %}
{% endif %}
{% endfor %}
{% for option in product.options_with_values %}
{% if option.name == 'Quantity' %}
{% for value in option.values %}
{% endfor %}
{% endif %}
{% endfor %}
Step 2. Add JavaScript to Display Prices for Each Variant:
Next, you’ll add JavaScript to retrieve the prices for each variant and display them in the respective placeholder.
-
In the Dawn theme, navigate to main-product.js or create a new JavaScript file if one doesn’t exist.
-
Write JavaScript to:
document.addEventListener("DOMContentLoaded", () => {
// Get variant data
const productVariants = {{ product.variants | json }};
// Add price to each option based on variant data
function updateVariantPrices() {
document.querySelectorAll(".size-option").forEach((sizeOption) => {
const size = sizeOption.getAttribute("data-size");
document.querySelectorAll(".quantity-option").forEach((quantityOption) => {
const quantity = quantityOption.getAttribute("data-quantity");
// Find the variant that matches the size and quantity
const matchingVariant = productVariants.find(
(variant) =>
variant.option1 === size && variant.option2 === quantity
);
if (matchingVariant) {
// Set the price in the span element within the button
sizeOption.querySelector(".variant-price").innerText = `$${matchingVariant.price}`;
quantityOption.querySelector(".variant-price").innerText = `$${matchingVariant.price}`;
}
});
});
}
// Run function on page load
updateVariantPrices();
// Optional: Update price on selection change if dynamic interaction needed
document.querySelectorAll(".size-option, .quantity-option").forEach((option) => {
option.addEventListener("click", () => {
// Re-run the function to update prices
updateVariantPrices();
});
});
});
- Add CSS for Styling (Optional):
To control the appearance of the prices, add custom CSS in theme.css or your preferred CSS file. For instance:
.variant-price {
font-size: 0.9em;
color: #666;
margin-left: 5px;
}
You can also hide the price for certain options (e.g., ‘Size’) if they shouldn’t be displayed by default.
If I was able to help you, please don’t forget to Like and mark it as the Solution!
If you’re looking for expert help with customization or coding, I’d be delighted to support you. Please don’t hesitate to reach out via the email in my signature below—I’m here to help bring your vision to life!
Best Regard,
Rajat Sharma
- Fetch the variant prices based on selected options (size and quantity).
- Display each variant’s price next to the corresponding button.
Here’s an example of how to achieve this: