【问题标题】:Adding 20% to product price via Javascript in Shopify在 Shopify 中通过 Javascript 为产品价格增加 20%
【发布时间】:2012-09-15 19:18:00
【问题描述】:

对此可能有一个简单的答案,有人可以帮助我,但我对 Javascript 不是很好。基本上在 Shopify 中,我已经将价格设置为使用他们的一种液体标签显示 +20%。除了在产品页面上,这很有效,因为如果尺寸缺货,有一些 Javascript 会禁用添加到购物车按钮,并将价格也更改为缺货。无论如何这里是代码:

<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。有谁知道这样做的方法?谢谢。

【问题讨论】:

    标签: javascript shopify


    【解决方案1】:

    试试这个

    <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
      }
    };
    

    【讨论】:

    • 我会的,我要等 8 分钟 :)
    • 我很好奇。你为什么要这样做?购物车不会认识到这种变化......那么将价格提高 20% 或其他有什么意义呢?
    • @DavidLazar 虽然这是不久前被问到的,但现在我要回答,因为它可能对其他人有帮助。客户只希望在产品页面上显示 1 个价格,并且由于他们在英国注册并且主要在欧盟销售,他们希望它显示 20% 的增值税,而不是让购物车上的增值税让人们感到惊讶。购物车上有 2 个总计,一个包含增值税,一个不包含非欧盟客户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-31
    相关资源
    最近更新 更多