最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Adding 20% to product price via Javascript in Shopify - Stack Overflow

programmeradmin4浏览0评论

There is probably a simple answer to this that someone can help me with but I'm not that great with Javascript. Basically in Shopify I've set up prices to be displayed +20% with one of their liquid tags. This works great apart from on the product page because there is a bit of Javascript that disables the add to cart button if a size is out of stock and changes the price to out of stock too. Anyway here is the code:

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

On the product listing pages I can add 20% to the price by using the following liquid tag

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

All I need to do is modify the Javascript so the price it outputs is multiplied by 1.20. Does anyone know of a way to do this? Thanks.

There is probably a simple answer to this that someone can help me with but I'm not that great with Javascript. Basically in Shopify I've set up prices to be displayed +20% with one of their liquid tags. This works great apart from on the product page because there is a bit of Javascript that disables the add to cart button if a size is out of stock and changes the price to out of stock too. Anyway here is the code:

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

On the product listing pages I can add 20% to the price by using the following liquid tag

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

All I need to do is modify the Javascript so the price it outputs is multiplied by 1.20. Does anyone know of a way to do this? Thanks.

Share Improve this question asked Sep 15, 2012 at 19:18 BantrosBantros 2958 silver badges17 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

Try this

<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.pare_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.pare_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
  }
};
发布评论

评论列表(0)

  1. 暂无评论