简体   繁体   English

全局液体变量在 JS 文件中不起作用

[英]Global Liquid Variable not working in JS file

I'am currently building a upsell section in the ajax cart (Brooklyn Theme).我目前正在 ajax 购物车(布鲁克林主题)中构建追加销售部分。 I need the variant.id of a product in my js file ** but have no idea how to do it.我需要我的js 文件中产品的variant.id **,但不知道该怎么做。 I tried everything I know.我尝试了我所知道的一切。

That's the code that works in every other file (except the js file):这是适用于所有其他文件的代码(js 文件除外):

{%- assign product = all_products[settings.upsell_product] -%}

{{ product.variants[0].id }}

The funny thing is that the following code works, even in the js file有趣的是下面的代码有效,即使在 js 文件中

{{ settings.upsell_headline }}

Thats my settings_scheme.json file code:这就是我的 settings_scheme.json 文件代码:

      {
        "type": "header",
        "content": {
          "de": "Cart Upsell"
        }
      },
      {
        "type": "text",
        "id": "upsell_headline",
        "label": {
          "de": "Headline"
        }
      },
      {
        "type": "product",
        "id": "upsell_product",
        "label": {
          "de": "Upsell Product"
        }
      }
    ]
  },

don't add code to the theme.js.liquid不要向theme.js.liquid添加代码

settings_scheme.json hold information about the theme. settings_scheme.json保存有关主题的信息。 All global data should be stored in settings_data.json .所有全局数据都应存储在settings_data.json In addition to that, the data is generated automatically from section when users customize the theme.除此之外,当用户自定义主题时,数据会自动从section生成。

https://shopify.dev/docs/themes/settings#basic-input-setting-types https://shopify.dev/docs/themes/settings#basic-input-setting-types

what you should do is, create a section called upsell-product.liquid , put the json there.你应该做的是,创建一个名为upsell-product.liquid的部分,将json那里。

<script>
  {%- assign product = all_products[section.settings.upsell_product] -%}

  $(document).ready(function(){
    console.log({{ product.variants[0].id }});
  });
</script>

{% schema %}
{   
  "name" : "upsell product",
  "settings" : [
    {
      "type" : "text",
      "id" : "headline",
      "label" : "headline",
      "default" : "headLine"
    },
     {
    "type": "product",
    "id": "upsell_product",
    "label": {
      "de": "Upsell Product"
    }
  ]  
}
{% endschema %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM