【问题标题】:Shopify price variation equationShopify 价格变化方程
【发布时间】:2021-06-02 08:56:03
【问题描述】:

我正在使用 Shopify Debut 主题,我想以某种方式在变体价格中添加增值税 (7%)。

静态价格我可以开始工作,但是当更改具有不同价格的变体时,hQuery 会覆盖这个,我不知道如何通过 jQuery 来改变它。

这是来自 theme.js 的 jQuery sn-p

/**
 * 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
  });
},

对于主题模板中的单一价格更改,我使用这个 sn-p,它可以工作,但不适用于 jQuery。

  {{  compare_at_price | times:1.07 | money }} 

theme.js的完整源文件

【问题讨论】:

    标签: jquery shopify shopify-template


    【解决方案1】:

    和液体中的一样,但是在 JS 中,找到这些行:

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

    添加替换它们:

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

    【讨论】:

    • 嗨弗拉基米尔,感谢您恢复这项工作,但每次您选择一个项目时它都会不断增加。怎么改成只做一次?
    • 尝试将该逻辑移近渲染方法,例如尝试将其移至variantPriceChange。由于我无权访问您的源文件,因此无法给您确切的答案。
    • 从问题中提到的方法中删除所有内容,然后找到这一行:“_updatePrice: function(evt) {”并放置我的代码(上面的两行)之后 该方法的第一行,以var variant = ...开头
    • 不知道你的意思,似乎工作正常,价格总是一样
    • 不错的弗拉基米尔!这似乎已经排序了
    猜你喜欢
    • 2022-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多