在Shopify中通过Javascript将产品价格提高20% [英] Adding 20% to product price via Javascript in Shopify

查看:26
本文介绍了在Shopify中通过Javascript将产品价格提高20%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能有一个简单的答案,有人可以帮助我,但我对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
  }
};

这篇关于在Shopify中通过Javascript将产品价格提高20%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆