如何使用shopify liquid获取url参数?
我有一个登录页,其中包含“添加到购物车”按钮,产品手柄如下:如何使用shopify liquid获取url参数?,shopify,liquid,Shopify,Liquid,我有一个登录页,其中包含“添加到购物车”按钮,产品手柄如下: shopifystorename.com/some-product-handle 我想要的是自动重定向,或者如果用户登陆上面的url,产品会自动添加到shopify购物车。我尝试过使用JS: let m = window.location.href; var n = m.lastIndexOf('/'); var result = m.substring(n+1); console.log(result);
shopifystorename.com/some-product-handle
我想要的是自动重定向,或者如果用户登陆上面的url,产品会自动添加到shopify购物车。我尝试过使用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 liquid上捕获,如下所示:
{% capture my_variable %}<span id="handle">test</span>{% endcapture %}
{{ my_variable }}
{{ all_products[my_variable].title }}
{%capture my_variable%}测试{%endcapture%}
{{my_variable}}
{{all_products[my_variable].title}
但是我没有得到值,或者它没有更新。它不起作用。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);