在Shopify中通过Javascript将产品价格提高20%

2022-03-02 00:00:00 shopify javascript

这个问题可能有一个简单的答案,有人可以帮助我,但我对Javascript不是很在行。基本上在Shopify,我用他们的一个液体标签设置了要显示的价格+20%。除了在产品页面上,这个功能非常有效,因为有一些Javascript可以在某个尺码脱销时禁用Add to Cart按钮,并将价格更改为脱销。不管怎样,下面是代码:

<script>
    var selectCallback = function(variant, selector) {
      if (variant && variant.available) {
        // valid variant selected
        $('#add-to-cart').removeClass('disabled').removeAttr('disabled').val('Add to Cart'); // remove unavailable class from add-to-cart button, and re-enable button
        if (variant.compare_at_price == 0){
          $('.product-title .price').html(''+Shopify.formatMoney(variant.price, "{{shop.money_format}}")+' Excluding VAT');
        } else {
          $('.product-title .price').html('<span>'+Shopify.formatMoney(variant.price, "{{shop.money_format}}") + '</span> <del>' + Shopify.formatMoney(variant.compare_at_price, "{{shop.money_format}}") + ' Excluding VAT</del>');
        }
      } else {
        // variant doesn't exist
        $('#add-to-cart').addClass('disabled').attr('disabled', 'disabled').val('Sold Out'); // set add-to-cart button to unavailable class and disable button
        var message = variant ? "Sold Out" : "Unavailable";
        $('.product-title .price').text(message); // update price-field message
      }
    };

在产品列表页上,我可以使用以下液体标签将价格增加20%

{{ product.price_min | times:1.20 | money }}

我需要做的就是修改Javascript,使其输出的价格乘以1.20。有谁知道做这件事的方法吗?谢谢。


解决方案

试试

<script>
var selectCallback = function(variant, selector) {
  if (variant && variant.available) {
    // valid variant selected
    $('#add-to-cart').removeClass('disabled').removeAttr('disabled').val('Add to Cart'); // remove unavailable class from add-to-cart button, and re-enable button
    if (variant.compare_at_price == 0){
      $('.product-title .price').html(''+Shopify.formatMoney((variant.price*1.2), "{{shop.money_format}}")+' Excluding VAT');
    } else {
      $('.product-title .price').html('<span>'+Shopify.formatMoney((variant.price*1.2), "{{shop.money_format}}") + '</span> <del>' + Shopify.formatMoney(variant.compare_at_price, "{{shop.money_format}}") + ' Excluding VAT</del>');
    }
  } else {
    // variant doesn't exist
    $('#add-to-cart').addClass('disabled').attr('disabled', 'disabled').val('Sold Out'); // set add-to-cart button to unavailable class and disable button
    var message = variant ? "Sold Out" : "Unavailable";
    $('.product-title .price').text(message); // update price-field message
  }
};

相关文章