如何访问 Shopify API in liquid 或 javascript variables in liquid?
How to access Shopify API in liquid or javascript variables in liquid?
据我所知,这似乎是不可能的。不过必须有办法做到这一点。我想要做的就是从 API 中获取一些信息,并将其传递给液体变量。也许还有别的办法。
由于 liquid 在服务器端呈现,因此您无法将 javascript 变量传递到模板引擎中。但是,您可以在 javascript 函数中嵌入 liquid 模板代码。 shopify 论坛上有一个 post,其中包含一些可能有帮助的示例代码:Pass variable from Java script to liquid?
例如:
$(function() {
new Shopify.OptionSelectors("product-select", { product: {{ product | json }}, onVariantSelected: selectCallback });
// Add label if only one product option and it isn't 'Title'.
{% if product.options.size == 1 and product.options.first != 'Title' %}
$('.selector-wrapper:eq(0)').prepend('<label>{{ product.options.first }}</label>');
{% endif %}
// Auto-select first available variant on page load.
{% assign found_one_in_stock = false %}
{% for variant in product.variants %}
{% if variant.available and found_one_in_stock == false %}
{% assign found_one_in_stock = true %}
{% for option in product.options %}
$('.single-option-selector:eq({{ forloop.index0 }})').val({{ variant.options[forloop.index0] | json }}).trigger('change');
{% endfor %}
{% endif %}
{% endfor %}
});
据我所知,这似乎是不可能的。不过必须有办法做到这一点。我想要做的就是从 API 中获取一些信息,并将其传递给液体变量。也许还有别的办法。
由于 liquid 在服务器端呈现,因此您无法将 javascript 变量传递到模板引擎中。但是,您可以在 javascript 函数中嵌入 liquid 模板代码。 shopify 论坛上有一个 post,其中包含一些可能有帮助的示例代码:Pass variable from Java script to liquid?
例如:
$(function() {
new Shopify.OptionSelectors("product-select", { product: {{ product | json }}, onVariantSelected: selectCallback });
// Add label if only one product option and it isn't 'Title'.
{% if product.options.size == 1 and product.options.first != 'Title' %}
$('.selector-wrapper:eq(0)').prepend('<label>{{ product.options.first }}</label>');
{% endif %}
// Auto-select first available variant on page load.
{% assign found_one_in_stock = false %}
{% for variant in product.variants %}
{% if variant.available and found_one_in_stock == false %}
{% assign found_one_in_stock = true %}
{% for option in product.options %}
$('.single-option-selector:eq({{ forloop.index0 }})').val({{ variant.options[forloop.index0] | json }}).trigger('change');
{% endfor %}
{% endif %}
{% endfor %}
});