gpt4 book ai didi

javascript - Shopify 液体 : How to only show a compare at price for a product variant if the compare at price exists?

转载 作者:行者123 更新时间:2023-11-28 02:44:56 25 4
gpt4 key购买 nike

我试图显示某些产品变体的价格比较,但我不知道如何仅在大于零的变体的价格比较时显示它。它显示的是 0.00 美元价格的比较。

它使用 JavaScript 来动态更新价格。这是代码:

<script type="text/javascript">
<!--
// mootools callback for multi variants dropdown selector
var selectCallback = function(variant, selector) {
if (variant && variant.available == true) {
// selected a valid variant
$('purchase').removeClass('disabled'); // remove unavailable class from add-to-cart button
$('purchase').disabled = false; // reenable add-to-cart button
$('price-field').innerHTML = Shopify.formatMoney(variant.price, "{{shop.money_with_currency_format}}"); // update price field
$('compare-price').innerHTML = Shopify.formatMoney(variant.compare_at_price, "{{shop.money_with_currency_format}}"); // update compare at price
} else {
// variant doesn't exist
$('purchase').addClass('disabled'); // set add-to-cart button to unavailable class
$('purchase').disabled = true; // disable add-to-cart button
$('price-field').innerHTML = (variant) ? "Sold Out" : "Unavailable"; // update price-field message
}
};

// initialize multi selector for product
window.addEvent('domready', function() {
new Shopify.OptionSelectors("product-select", { product: {{ product | json }}, onVariantSelected: selectCallback });
});
-->
</script>

当我有一个原始的 <div id="compare-price"></div> 时,脚本工作正常。 。每个变体的动态价格比较都会添加到该 div 中。但如果没有价格比较,它仍然会增加 0.00 美元。

当没有针对变体的价格比较时,如何隐藏 div?我正在尝试做这样的事情:

{% if product.variant.compare_at_price %}
<div id="compare-price"></div>
{% endif %}

有人知道怎么做吗?

最佳答案

好吧,这就是我要做的工作。可能不是最优雅的解决方案,但它正在工作:

<script type="text/javascript">
<!--
// mootools callback for multi variants dropdown selector
var selectCallback = function(variant, selector) {
if (variant && variant.available == true) {
// selected a valid variant
$('purchase').removeClass('disabled'); // remove unavailable class from add-to-cart button
$('purchase').disabled = false; // reenable add-to-cart button
$('price-field').innerHTML = Shopify.formatMoney(variant.price, "{{shop.money_with_currency_format}}"); // update price field
if(variant.compare_at_price > 0.0) {
$('compare-price').innerHTML = Shopify.formatMoney(variant.compare_at_price, "{{shop.money_with_currency_format}}"); // update compare at price
} else {
$('compare-price').innerHTML = "";
}
} else {
// variant doesn't exist
$('purchase').addClass('disabled'); // set add-to-cart button to unavailable class
$('purchase').disabled = true; // disable add-to-cart button
$('price-field').innerHTML = (variant) ? "Sold Out" : "Unavailable"; // update price-field message
}
};

// initialize multi selector for product
window.addEvent('domready', function() {
new Shopify.OptionSelectors("product-select", { product: {{ product | json }}, onVariantSelected: selectCallback });
});
-->
</script>

关于javascript - Shopify 液体 : How to only show a compare at price for a product variant if the compare at price exists?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12062285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com