Shopify-更改我的selectorOption产品下拉变体的文本
Shopify - Change text of my selectorOption product dropdown variant
我花了一整天的时间研究它,但不可能弄清楚,所以我需要一些帮助:)
我试着在我的产品页面上的"尺寸"下拉列表的每一行添加一些文本:
如果产品数量>0:尺寸+快速交付其他:尺寸+2-3周交货我只是想在点击之前向客户显示它,所以我不想只在selectedVariant上显示它。
我试图通过我的script.js来改变它,我想:
复制每个变体数量(我没有找到方法)
在列表值/键中复制我的下拉列表+添加文本(快速/2-3周),具体取决于变量数量var optionsSize={};
var optionsSizes = {};
$('#mySizes option').each(function(){
optionsSizes[$(this).val()] = $(this).text() + variant.inventory_quantity;
});
//console.log(optionsSizes);
var $el = $("#mySizes");
$el.empty(); // remove old options
$.each(optionsSizes, function(value,key) {
$el.append($("<option></option>")
.attr("value", value).text(key));
});
下拉列表的复制/粘贴工作,但仅此而已。在variantSelected上做这件事很容易,但这不是我想要的。
如果您有任何问题,请随时询问。
干杯,
bkseen
('#product-select-5916642311-option-0')
和$('#mySizes')
默认情况下,此select
元素不在主题中。Shopify或主题脚本根据Shopify提供的产品JSON信息添加这两个元素。因此,没有直接的方法来获得期望的结果。
这里有一个技巧可以实现你的愿望。
- 将所有变体及其所需属性加载到JSON对象中。为此,请在液体文件的顶部添加
<script>variantsJSON = {}</script>
-
现在以以下结构加载变体:
variantsJSON = { "variant_option1" : { "variant_option2_1_title" : "variant_option2_1_quantity", "variant_option2_2_title" : "variant_option2_2_quantity", } .... }
-
要获得上述结构,您需要在
{% for variant in product.variants %}
中添加以下脚本,或者在您的液体文件中添加类似的循环。
<script>
if (!variantsJSON['{{ variant.option1 }}']){
variantsJSON['{{ variant.option1 }}'] = {}
}
{% assign availability = 'QUICK DELIVERY' %}
{% if variant.inventory_quantity == 0 %}{% assign availability = '2-3 WEEKS DELIVERY' %}{% endif %}
if (!variantsJSON['{{ variant.option1 }}']['{{ variant.option2 }}']){
variantsJSON['{{ variant.option1 }}']['{{ variant.option2 }}'] = '{{ availability }}'
}
</script>
-
上面的片段(可能需要细化)将把所有变体及其可用性加载到JSON对象中
-
现在,您只需要在
$('#product-select-{{ product.id }}-option-0')
发生更改时触发一个函数,该函数将清除$('#mySizes')
中的所有<li>
,然后使用variantsJSON的variant_option2&所选variant_option1的variant_availability
p.S.请随意格式化我的答案。不知怎么的,我无法获得正确的格式
在上回答赞美诗
这很棘手,但我想我能帮上忙。您是如何用类product__inventory-count product__form-message更改跨度的?–赞美诗10小时前区块报价
if (variant) {
{% if settings.product_show_shipping %}
var myCount = variant['inventory_quantity'];
var myPath = this.element.find('.product__inventory-count');
if (myCount < 1){
myPath.removeClass('product__form-message--success');
myPath.addClass('product__form-message--error');
myPath.text("2-3 weeks delivery");
}else{
//myPath.toggleClass('product__form-message--success').siblings().removeClass('checked');
myPath.removeClass('product__form-message--error');
myPath.addClass('product__form-message--success');
myPath.text("Quick delivery");
}
{% endif %}
问题是,变体是所选择的变体,而不是经过所有产品的变体。
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 在我的网站上以纯文本形式添加最新推文
- 为什么我的文本框没有用查询结果更新
- 可以't使我的文本值在查询中起作用
- 无法使我的文本参数与我的查询一起工作
- 有人能发现我的错误吗(自动完成文本框)
- 我的剧本赢了'不能在IE中工作(甚至9)?修改复制文本的简单javascript
- 为什么我的导航按钮或按钮文本不会移动到中心
- 文本长度限制在我的 instafeed 模板中不起作用
- Angularjs:如何捕获
标签中的文本并在我的控制器中使用它 - 我的文本区域没有正确记录输出
- 我想限制用户在jquery中输入文本后面的空格,如果它在文本后面,我会删除空格
- 如何将图像放入我的滚动文本js中
- 与我的jquery弹出窗口和自动调整文本区域大小有冲突
- 水平滚动Marquee,当函数被调用时,我的文本坚持聚集在一起
- 我的价值赢得't文本居中对齐
- Shopify-更改我的selectorOption产品下拉变体的文本
- 如何设置元素的占位符文本 我无法编辑 HTML 并且 ID 正在更改
- 我正在尝试将javascript文本框的输入保存回服务器.我想从保存的弹出文本框中获得输入,以重命名文件夹
- Three.js-如何使我的文本精灵更清晰