【问题标题】:Disable checkout if all products do not share same tag Shopify如果所有产品不共享相同的标签 Shopify,则禁用结帐
【发布时间】:2020-10-16 13:26:39
【问题描述】:

我的 Shopify 商店有自定义结账体验,我只想在购物车中的所有产品都包含“test”标签时允许这种体验

这是我目前拥有的功能,它似乎只适用于购物车中的单个商品。

  function productTags() {
    {%- assign tagEnabled = false -%}
    return {
      {%- for item in cart.items -%}
        {%- if item.product.tags contains 'test' -%}
          "{{ item.product.tags }}": {{ item.quantity | json }}
          {%- assign tagEnabled = true -%} 
        {%- endif -%}
      {%- endfor -%}
    };
  }     

此行(“{{ item.product.tags }}”:{{ item.quantity | json }})仅在测试时在控制台中显示。如有必要,我可以删除它。

如何扩展它以查找购物车中的所有商品标签,并且只有在它们都具有相同标签时才将 tagEnabled 变量分配为 true?

提前致谢!

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    您似乎正在尝试将 Liquid 与 Javascript 混合使用,这可能会使编码变得混乱。我建议将您的代码分成两部分:一是您收集 Liquid 变量并将它们分配给 Javascript 变量(我看到您已经使用 json 过滤器来执行此操作,这太棒了 - 该过滤器保证您的变量输出将采用 Javascript 合法格式),第二部分是您编写 Javascript 代码而没有任何 Liquid 括号妨碍。 (如果您使用带有任何语法或错误突出显示的代码编辑器,这将特别有用)

    如果以下内容有助于您获得所需信息,请告诉我:

    // The Liquid filter 'map' lets you drill into nested object properties. We can use this to get an array of tag arrays.
    // The JSON Liquid filter will turn any Liquid variable into a legal Javascript variable.
    const itemProductTags = {{ cart.items | map: 'product'| map: 'tags' | json }}
    
    // Check on our variables
    console.log('All product tag arrays', itemProductTags)
    console.log('Results of tagCheck()', tagCheck(itemProductTags, 'test') )
    
    function tagCheck(tagsList, targetTag){
      // Checks to see if the targetTag appears in all, some or none of the tag arrays.
      // tagsList will be an array of arrays
    
      var targetInAny = false;
      var targetInAll = true;
    
      for(var i=0; i<tagsList.length; i++){
        var tags = tagsList[i];
        if(tags.indexOf(targetTag) != -1){
          // Found the tag - this targetTag appears in at least one product-tag list
          targetInAny = true;
        }
        else{
         // Did not find the tag - this targetTag is NOT in every product-tag list
         targetInAll = false;
        }
      }
    
      // Returns an object specifying if we found the target in any, all or none of the lists
      return { targetInAny: targetInAny, targetInAll: targetInAll, targetInNone: !targetInAny }
    }
    

    【讨论】:

    • 我想这给了我我需要的东西,这是一个很大的帮助!如果我能再问一个非常愚蠢的问题,那就太棒了。我只想在 targetInAll 为真时才调用我的 inject() 函数。您认为调用该函数并将 targetInAll 的结果存储在液体变量中的最佳调用是什么?
    • 在服务器端处理液体代码以创建提供给客户端计算机的 HTML/CSS/Javascript 文件。下载这些文件后,客户端的浏览器将运行提供的代码。长话短说,您可以将 Liquid 变量传递给 Javascript(发生在服务器级别),但您不能使用 Javascript 在 Liquid 中分配变量(因为 Javascript 在客户端运行)。也就是说,您可以通过多种方式使用 Javascript 呈现您想要的部分...
    • ... 其中最简单的可能是使用新的部分渲染 API (shopify.dev/docs/themes/sections/section-rendering-api),它允许您使用 Javascript 根据部分名称获取部分的渲染 HTML 内容。
    • 请注意,您可以使用此方法获取某个部分的 HTML,而不管该部分是否已包含在任何给定页面上。这使您可以简单地创建该部分,而不是直接将其包含在任何地方 - 您将通过 javascript 请求访问呈现的内容。
    • Dave B... 我得把它交给你。您在 Shopify 论坛上的任何人之前回复了这里。它非常有帮助和有用,并为我手头的问题提供了确切的解决方案。非常感谢您,希望您周末愉快。
    猜你喜欢
    • 2022-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多