Search code examples
jqueryshopifyshopify-template

Shopify price variation equation


I am using the Shopify Debut theme and I would like to somehow add vat (7%) to the price of a variation.

Static price I can get working but when changing a variation that has a different price hQuery overwrites this and I don't know how to change this through jQuery.

This is the jQuery snippet from theme.js

/**
 * Trigger event when variant price changes.
 *
 * @param  {object} variant - Currently selected variant
 * @return {event} variantPriceChange
 */
_updatePrice: function(variant) {
  if (
    variant.price === this.currentVariant.price &&
    variant.compare_at_price === this.currentVariant.compare_at_price
  ) {
    return;
  }

  this.$container.trigger({
    type: 'variantPriceChange',
    variant: variant
  });
},

For single price change in the theme template i use this snippet which works but am not good with jQuery.

  {{  compare_at_price | times:1.07 | money }} 

Full source file of theme.js

enter image description here


Solution

  • Same as in liquid, but in JS, find these lines:

      // On sale
      if (variant.compare_at_price > variant.price) {
        $regularPrice.html(
          theme.Currency.formatMoney(
            variant.compare_at_price,
            theme.moneyFormat
          )
        );
        $salePrice.html(
          theme.Currency.formatMoney(variant.price, theme.moneyFormat)
        );
        $priceContainer.addClass(this.classes.productOnSale);
      } else {
        // Regular price
        $regularPrice.html(
          theme.Currency.formatMoney(variant.price, theme.moneyFormat)
        );
      }
    

    add replace them with this:

    // On sale
      if (variant.compare_at_price > variant.price) {
        $regularPrice.html(
          theme.Currency.formatMoney(
            variant.compare_at_price * 1.07,
            theme.moneyFormat
          )
        );
        $salePrice.html(
          theme.Currency.formatMoney(variant.price * 1.07, theme.moneyFormat)
        );
        $priceContainer.addClass(this.classes.productOnSale);
      } else {
        // Regular price
        $regularPrice.html(
          theme.Currency.formatMoney(variant.price * 1.07, theme.moneyFormat)
        );
      }