Adding 20% to product price via Javascript in Shop

2019-04-14 07:13发布

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

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.

1条回答
别忘想泡老子
2楼-- · 2019-04-14 08:07

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.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
  }
};
查看更多
登录 后发表回答