首页 > 解决方案 > 检查 JS 远程替换、Shopify 中的变体可用性

问题描述

我被要求定制一个 Shopify 网站,但我在使用 Variants Dropdown 时遇到了问题,因为有一个简短的 JS 代码片段替换了主要的 Select 元素。

此片段不检查变体可用性,我不知道 JS,所以想知道是否有人有解决方案?

我尝试在 JS 代码中使用 IF 语句来实现结果,但由于某种原因它破坏了页面的标题并且不显示变体数量。

如前所述,我不了解 JS,所以希望这是相当直截了当的事情。

提前致谢。

这是指向此问题的页面的链接。 https://perfumesonline.ie/products/3614223162963?variant=31708294938676

下面是JS代码

// RENDER SIZE OPTION
var renderSizeOptions = function(options){
    var sizeOptionIndex = $('.size_product__options .size_toggle').data('option-index');
    var sizeSelect = $('#shopify-section-' + sectionID + ' .single-option-selector').eq(sizeOptionIndex);
    var selectId = '#' + sizeSelect.attr('id');
    var container = $('#shopify-section-' + sectionID + ' .size_product__options .size_toggle');
    var content = '<label>' + options[sizeOptionIndex] + ':</label>';

    
    $( selectId + ' option' ).each(function(){
        var value = $(this).val();
        sizeSelect.parent('.selector-wrapper').addClass('hidden');
        if ( sizeSelect.val() == value ) {
            return content = content + '<div class="size_item current" data-val="' + value + '"><span class="size_inner">' + value + '</span></div>';
        } else {
            return content = content + '<div class="size_item" data-val="' + value + '"><span class="size_inner">' + value + '</span></div>';
        };
    } );

    container.html(content);

    $('.size_product__options .size_item').on('click', function(e){
        sizeSelect.val( $(this).data('val') ).trigger('change');
    });

};

标签: javascriptshopifyvariant

解决方案


推荐阅读