【问题标题】:Shopify - Issues with Jquery Function for displaying weightShopify - 显示重量的 Jquery 函数问题
【发布时间】:2023-03-20 03:29:02
【问题描述】:

你们今天好吗?

在我的产品页面 (product.liquid) 上编码时,我需要您的帮助。需要在变体单选按钮内显示重量 + 重量转换单位。

product.liquid 上的 Jquery 变体函数:

jQuery(function($) {
    {% if product.available and product.variants.size >= 1 %}
    var product = {{product | json}};
    var layout = $('.product-page-area');
    if (product.variants.length >= 1) { //multiple variants
      for (var i = 0; i < product.variants.length; i++) {
        var variant = product.variants[i];
        var option = '<option value="' + variant.id + '">' + variant.title + '</option>';
        layout.find('form.product-form > select').append(option);
      }
      new Shopify.OptionSelectors("product-selectors", {
        product: product,
        onVariantSelected: selectCallback,
        enableHistoryState: true
      });

      //start of quickview variant;
      var filePath = asset_url.substring(0, asset_url.lastIndexOf('/'));
      var assetUrl = asset_url.substring(0, asset_url.lastIndexOf('/'));
      var options = "";
      for (var i = 0; i < product.options.length; i++) {
        options += '<div class="swatch clearfix" data-option-index="' + i + '">';
        options += '<div class="header">' + product.options[i] + ':</div>';
        options += '<div class="variant-items">';
        var is_color = false;
        var is_square = 'square';
        if (/Color|Colour/i.test(product.options[i])) {
          is_color = true;
        }
        if (swatch_color_type == '2') {
          is_color = false;
          is_square = '';
        }
        var optionValues = new Array();
        for (var j = 0; j < product.variants.length; j++) {
          var variant = product.variants[j];
          var value = variant.options[i];
          if(variant.featured_image && product_swatch_setting == '2') {
            var img = variant.featured_image.src.lastIndexOf(".");
            var vimg = variant.featured_image.src.slice(0, img) + "_50x50_crop_center" + variant.featured_image.src.slice(img);
          }
          var valueHandle = convertToSlug(value);
          var forText = 'swatch-' + i + '-' + valueHandle;
          if (optionValues.indexOf(value) < 0) {
            //not yet inserted
            options += '<div data-value="' + value + '" class="swatch-element '+is_square+' '+product_swatch_size+' '+(is_color ? "color" : "")+' ' + (is_color ? "color" : "") + valueHandle + (variant.available ? ' available ' : ' soldout ') + '">';
            
            if (is_color) {
              options += '<div class="tooltip">' + value + '</div>';
            }
            options += '<input id="' + forText + '" type="radio" name="option-' + i + '" value="' + value + '" ' + (j == 0 ? ' checked ' : '') + (variant.available ? '' : ' disabled') + ' />';

            if (is_color) {
              if(vimg && product_swatch_setting == '2') {
                options += '<label for="' + forText + '" class="swatch-image" style="overflow:hidden;"><img src="' + vimg + '" class="variant-image" style="max-width:100%;" /><img class="crossed-out" src="' + assetUrl + 'soldout.png" /></label>';
              }else{
                options += '<label for="' + forText + '" style="background-color: ' + valueHandle + '; background-image: url(' + filePath + valueHandle + '.png)"><img class="crossed-out" src="' + assetUrl + 'soldout.png" /></label>';
              }
            } else {
              options += '<label class="' + value + '" for="' + forText + '">' + '<span class="variant-button-top"></span>' + variant.weight + '<span class="variant-button-bottom"></span>' + '<img class="crossed-out" src="' + assetUrl + 'soldout.png" /></label>';
            }
            options += '</div>';
            if (variant.available) {
              $('.product-page-area .swatch[data-option-index="' + i + '"] .' + valueHandle).removeClass('soldout').addClass('available').find(':radio').removeAttr('disabled');
            }
            optionValues.push(value);
          }
        }
        options += '</div>';
        options += '</div>';
      }
      if(swatch_color_type == '1' || swatch_color_type == '2') {
        layout.find('form.product-form .product-options > select').after(options);
        layout.find('.swatch :radio').change(function() {
          var optionIndex = $(this).closest('.swatch').attr('data-option-index');
          var optionValue = $(this).val();
          $(this)
          .closest('form')
          .find('.single-option-selector')
          .eq(optionIndex)
          .val(optionValue)
          .trigger('change');
        });
      }
  
      if (product.available) {
        Shopify.optionsMap = {};
        Shopify.linkOptionSelectors(product);
      }
      //end of quickview variant
    } else { //single variant
      layout.find('form.product-form .product-options > select').remove();
      var variant_field = '<input type="hidden" name="id" value="' + product.variants[0].id + '">';
      layout.find('form.product-form').append(variant_field);
    }
    {% endif %}
  });

在这一行:

+ '<span class="variant-button-top"></span>' + variant.weight + '<span class="variant-button-bottom"></span>' +

我想使用:variant.weight | weight_with_unit:variant.weight_unit

但它不起作用。即使我使用:+ variant.weight + variant.weight_unit + 它返回未定义。

我想返回变体重量和变体转换单位。

你能帮帮我吗?提前谢谢!

【问题讨论】:

  • 我认为您需要将值分配给循环内的变量,然后在 JS 代码中使用它。
  • 感谢您的反馈。稍后再试一试:)
  • 如果问题仍然存在,请告诉我
  • 在 jquery 中,我创建了一个新变量: var pweight = {{variant.weight | weight_with_unit | json}} 但是当我调用它时,它仍然没有显示重量单位。只有重量值。无法理解这一点。
  • + '&lt;span class="variant-button-top"&gt;&lt;/span&gt;' + variant.weight + ' '+variant.weight_unit+'&lt;span class="variant-button-bottom"&gt;&lt;/span&gt;' +

标签: jquery shopify


【解决方案1】:

在您的情况下,主题使用{{product | json }} 将 JSON 数据转换为 javascript 代码,可以遗憾地说,但 JSON 错过了变体重量单位,您无法在循环中获得相同的数据。

要克服这种情况,您需要使用产品句柄获取 JSON 数据,该数据包含重量单位以及其他数据。

我希望这对您将来遇到类似问题的其他开发人员有所帮助。

【讨论】:

    【解决方案2】:

    如果您为变体分配了权重,请使用 Liquid 来显示它。您可以在 Liquid 中进行简单的数学运算。跳过所有 Javascript 废话,直接使用 Liquid。它就是为此而生的。根据您在此处描述的内容,您无需动态编写此客户端的脚本。

    【讨论】:

    • 我不是开发人员。这是我买的一个主题。不幸的是我只知道html和css。 JavaScript 和其他语言我通常会一起挑选一些代码,直到实现我想要的。需要扩展我的知识。打算试一试。我认为这可能是一个简单的解决方案。
    • 最简单的事情就是把 10 年前有人写的 97 行古老的 jQuery 替换成能正确显示你体重的一行 Liquid。因此,如果您正在寻找简单,那就尽可能简单。
    猜你喜欢
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多