首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shopify -更改我的selectorOption产品下拉变体的文本

Shopify -更改我的selectorOption产品下拉变体的文本
EN

Stack Overflow用户
提问于 2016-04-08 02:30:06
回答 2查看 1.3K关注 0票数 0

我花了一整天的时间想弄清楚,所以我需要一些帮助:)

我试着在产品页面的“大小”下拉列表的每个变体行上添加一些文本:

如果产品数量>0: Size +快速交付,否则: Size + 2-3周交付只想在点击之前向客户显示,所以我不希望它只出现在selectedVariant上。

我试图通过我的script.js来改变它,我在想:

复制每个变量的数量(我没有找到方法)

根据变量变量var optionsSizes ={},在列表值/key+ add文本(Quick/2-3周)中复制我的下拉列表;

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-02 06:34:37

('#product-select-5916642311-option-0')$('#mySizes') --默认情况下,select元素不在主题中。Shopify或主题脚本根据通过Shopify获得的产品的JSON信息添加这两个元素。因此,没有直接的方法来获得想要的结果。

这里有一个技巧,可以实现你想要的。

  1. 将所有变体及其所需的属性加载到JSON对象中。为此,在液体文件的顶部添加<script>variantsJSON = {}</script>
  2. 现在,按以下结构加载这些变体: variantsJSON = { "variant_option1" : { "variant_option2_1_title" : "variant_option2_1_quantity", "variant_option2_2_title" : "variant_option2_2_quantity", } .... }
  3. 要获得上述结构,您需要在{% 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>

  1. 上面的片段(可能需要细化)将将所有变体及其可用性加载到JSON对象中。
  2. 现在,您所需要做的就是在更改variantsJSON's variant_option2时触发一个函数,该函数将清除$('#mySizes')中的所有<li>,然后使用所选variant_option1variant_option1& variant_availability中存储的值填充它们。

请随意给我的答案打格式。我不知何故无法得到正确的格式。

票数 1
EN

Stack Overflow用户

发布于 2016-04-29 02:19:11

回答希姆兹

这很棘手,但我想我能帮上忙。您是如何使用产品_库存-计数产品__表单-消息-成功?- 10小时前的HymnZ类更改广度的?

代码语言:javascript
复制
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 %}

问题是,变体是选择的变体,而不是通过所有产品的变体。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36490520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档