I’m having trouble with a custom variant picker I built for my Shopify product pages. The default variant selector works perfectly, but my custom one won’t update the product price when customers select different options. I suspect there’s an issue with my JavaScript implementation.
var variantCallback = function(selectedVariant, picker) {
var $cartButton = $('#add-to-cart-btn'),
$buyNowButton = $('#buy-now-btn'),
$stickyCartBtn = $('#sticky-add-cart'),
$priceDisplay = $('#product-price'),
$originalPrice = $('#original-price'),
$stockQuantity = $('#stock-amount'),
$qtySelectors = $('.qty-picker, label + .quantity-input'),
$cartButtonText = $('#add-to-cart-btn'),
$mainImage = $('#main-product-image');
if (selectedVariant) {
// Update product price regardless of inventory status
Shopify.money_format = '{{shop.money_format}}';
var accessiblePrice = Shopify.formatMoney(selectedVariant.price, Shopify.money_format);
var priceMarkup = ' <span class="sr-only">' + accessiblePrice + '</span>';
$priceDisplay.html(priceMarkup);
// Handle compare at price display if enabled
{% if settings.show_compare_price %}
if (selectedVariant.compare_at_price > selectedVariant.price) {
var accessibleComparePrice = Shopify.formatMoney(selectedVariant.compare_at_price, Shopify.money_format);
var comparePriceMarkup = ' <span class="sr-only">' + accessibleComparePrice + '</span>';
$originalPrice.html(comparePriceMarkup);
$originalPrice.show();
} else {
$originalPrice.hide();
}
{% endif %}
}
};
jQuery(function($) {
var productData = {{ product | json }};
new Shopify.OptionSelectors('variant-selector', {
product: productData,
onVariantSelected: variantCallback,
enableHistoryState: true
});
});
Can anyone spot what might be wrong with this code? The variant selection isn’t triggering price updates.