【问题标题】:how to get url parameters using shopify liquid?如何使用 shopify 液体获取 url 参数?
【发布时间】:2020-11-09 09:54:50
【问题描述】:

我有一个包含添加到购物车按钮的登录页面,产品句柄如下:

shopifystorename.com/some-product-handle

我想要的是自动重定向或产品自动添加到 shopify 购物车,如果用户登陆上面的那个 url。我尝试过使用 JS:

  let m = window.location.href;
  var n = m.lastIndexOf('/');
  var result = m.substring(n+1);
  
  console.log(result);
  var span = document.getElementById("handle");
  span.textContent = result;

然后像这样在 shopify 液体上捕获它:

{% capture my_variable %}<span id="handle">test</span>{% endcapture %}
{{ my_variable }}

{{ all_products[my_variable].title }}

但是我没有得到值或者它没有更新。

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    这行不通。 Liquid 用于服务端渲染引擎,JS 用于前端。您需要在这里纯粹使用 Javascript 来使用 Shopify 的 AJAX API,并以更精致和简单的方式。

    var thisProduct = await fetch('/products/'+ location.pathname +'.js').then(p => { return p.json(); });
    alert('The title of this product is ' + thisProduct.title);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-18
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 2021-03-27
      相关资源
      最近更新 更多