【问题标题】:Anyway of using Shopify metafield in js.liquid file?无论如何在 js.liquid 文件中使用 Shopify 元字段?
【发布时间】:2018-09-12 09:51:10
【问题描述】:

我想改变我的产品数量选择器的工作方式。

目前它以 1 递增,这是标准的。但是,我所有的产品都有不同的数量。

所以我为它们设置了一个 int 值的元字段。

所以在普通的液体文件中,我可以执行{{ product.metafields.qty_incr.qty-incr }} 并显示该值。在这个例子中,5.

我怎样才能让它在我的 .js.liquid 文件中工作?

// Add or subtract from the current quantity
  if ($el.hasClass('ajaxcart__qty--plus')) {
    qty += {{ product.metafields.qty_incr.qty-incr }};
  } else {
    qty -= {{ product.metafields.qty_incr.qty-incr }};
    if (qty <= 0) qty = 0;
  }

我做了上面的,这是行不通的。可能是新手,我不能在 .js.liquid 文件中使用液体。

【问题讨论】:

    标签: javascript shopify liquid


    【解决方案1】:

    您可以在其中一个主题文件中创建一个全局 js 变量,如下所示:

    <script>const productMetaQtyIncr = {{ product.metafields.qty_incr.qty-incr }}</script>
    

    然后在.js文件中使用:

    // Add or subtract from the current quantity
      if ($el.hasClass('ajaxcart__qty--plus')) {
        qty += productMetaQtyIncr;
      } else {
        qty -= productMetaQtyIncr;
        if (qty <= 0) qty = 0;
      }
    

    【讨论】:

    • 非常感谢! - 完美运行 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多