【问题标题】:Trying to display related products using a specific product tag on Shopify尝试在 Shopify 上使用特定产品标签显示相关产品
【发布时间】:2016-02-17 12:54:56
【问题描述】:

我目前正在使用脚本在我管理的 Shopify 网站上输出相关产品,但我想修改此脚本以获取当前产品的“颜色”标签(例如“Color_Jet Black (#1)”)然后使用这个标签生成也有这个标签的相关产品。

下面的代码是我目前正在使用的代码。

<div class="related-products">
    <div class="container-fluid nopadding">
        <div class="row grid">

        </div>
    </div>
</div>
<script>
$( document ).ready(function() {
    // Variables
      var related = $('.related-products');
      var handle = "{{ product.handle }}";
      var tags = [];
      var appendLocation = $(".related-products .row");
      var relatedProductsList = [];
      var maxOutput = 4
      var count = 0;
    // Get Product Tags
    {% for tag in product.tags %}
      tags.push("{{ tag }}");
}
    {% endfor %}
    // Get Run JSON product tags against this product's tags
    jQuery.getJSON( document.location.origin + "/products.json", function(obj) {
        // Build List
        jQuery.each(obj.products, function(dontcare, product) {
            hasMatch(tags, product);
        });
        // Shuffle List
        shuffle(relatedProductsList);
        // Output List
        jQuery.each(relatedProductsList, function( dontcare, product) {
            if(count < maxOutput) {
                outputMatch(product);
            }
            count++;
        });
    }).complete(function() {
        // Wait for products to load
        $('.product-item img').load(function() {
            var item = $('.product-item');
            setProductItemHeight(item);
        });
    });
    // Checks for a match between product tags and JSON tags
    function hasMatch(tags, product) {
        jQuery.each(tags, function(dontcare, tag) {
            if(jQuery.inArray(tag, product.tags) >= 0 && product.handle != handle) {
                relatedProductsList.push(product);
            }
        });
    }
    // Outputs matches
    function outputMatch (product) {
        // If product has a featured image
        if(product.images[0] != null) {
            // Create Item
            $item = $("<div class='col-md-3'><article class='product-item'><a href=" +
                document.location.origin + "/products/" +
                product.handle +"><div class='image-wrapper'><img class='img-responsive' src='" +
                product.images[0].src + "'></div><div class='product-wrapper prod-caption caption'><h6>" +
                product.title + "</h6></a><p>" + product.variants[0].price + "</p><a class='btn btn-default' href=" + document.location.origin + "/products/" + product.handle +">Buy Now</a></div></article></div>");
            appendLocation.append($item);
        } else if (product.images[0] == null) {
            // Fallback if product object has no images
            appendLocation.append("<div class='col-md-3'><article class='product-item'><a href=" + document.location.origin + "/products/" + product.handle +"><div class='image-wrapper'><img class='img-responsive' src='//cdn.shopify.com/s/files/1/0878/7440/t/2/assets/default.png'></div><div class='product-wrapper prod-caption'><h3>" + product.title + "</h3><p>" + product.tags[0] + "</p><em>" + product.variants[0].price + "</em><br><a class='btn btn-default' href=" + document.location.origin + "/products/" + product.handle +">Buy Now</a></div></article></div>");
        }
    }
    // Randomizes relatedProductsList
    function shuffle(relatedProductsList) {
        for (var n = 0; n < relatedProductsList.length - 1; n++) {
            var k = n + Math.floor(Math.random() * (relatedProductsList.length - n));
            var temp = relatedProductsList[k];
            relatedProductsList[k] = relatedProductsList[n];
            relatedProductsList[n] = temp;
        }
    }
});
</script>

我尝试修改代码以获取当前的产品颜色标签,这可行,但它不会像原始脚本那样输出任何产品。有很多产品都有这个颜色标签,所以这不是问题。

下面的代码是我用来抓取当前产品的颜色标签的,它确实有效,但它在 DOM 中没有输出。

$( document ).ready(function() {
    // Variables
      var related = $('.related-products');
      var handle = "{{ product.handle }}";
      var tags = [];
      var appendLocation = $(".related-products .row");
      var relatedProductsList = [];
      var maxOutput = 4
      var count = 0;
    // Get Product Tags
    {% assign color = "Color_" %}
    {% for tag in product.tags %}
      {% if tag contains color %}
        tags.push("{{ tag }}");
}
      {% endif %}
    {% endfor %}

任何想法/帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery shopify liquid


    【解决方案1】:

    一种方法是创建一个输出 json 的新集合模板,并使用“view”参数将该模板应用于您的 json 请求。

    “视图”参数仅允许您动态使用不同的模板来呈现内容。 https://docs.shopify.com/manual/configuration/store-customization/page-specific/collections/add-view-all-to-collection-pages

    这是一个示例 json 液体模板(在我的脑海中完成 - 您需要检查要使用哪些液体标签):

       {% layout none %} // tells shopfify not to render layout template
       {
          "products": [
            {% for product in collection.products %}
        ...
         {
            "id": "{{product.id}}",
            "title": "{{product.title}}",
            "handle": "{{product.handle}}",
            "image" : "{{product.featured_image}}",
            "price" : "{{product.price}}"
        }
        {% if forloop.last == false %},{% endif %} ... don't forget the , - it mustn't be on the last entry.
        ....
            {% endfor %}
          ]
        }
    

    因此,当模板被渲染时 - 你会得到一个格式为 json 的产品列表。像这样的:

    {
      "products": [
        {
          "id": "12234",
          "title": "foo",
          "handle": "bar",
          "image": "blah.jpg",
          "price": "12345"
        },
        {
          "id": "12235",
          "title": "foo1",
          "handle": "bar1",
          "image": "blah1.jpg",
          "price": "12346"
        },
        {
          "id": "12239",
          "title": "foo2",
          "handle": "bar2",
          "image": "blah2.jpg",
          "price": "12375"
        }
      ]
    }
    

    在发出 ajax 请求时,您需要请求带有标签和视图的 URL。所以,如果你调用你的新模板 json.liquid 那么你会请求:

    /collections/frontpage/Color_Jet-Black-(#1)?view=json
    

    例如:

    jQuery.getJSON( '/collections/frontpage/Color_Jet-Black-(#1)?view=json", function(obj) {
    

    所以在这个例子中 - 'Color_Jet Black (#1)' 是标签。您应该只收到 Frontpage 系列中标有 Color_Jet Black (#1) 的产品。因为我们将?view=json 附加到我们的请求中,Shopify 将使用我们创建的 json 模板发送这些产品*。然后,您可以随意随机化该 json 并通过 JavaScript 将其呈现到页面。

    • 请注意,在我的示例中,我将新集合模板称为 json.liquid,因为它用于渲染 Json - 但您可以随意调用它。注意:'featured_products.liquid'。但是,由于它用于输出 Json,因此将其称为 json.liquid 是有意义的。

    Shopify 集合 url 采用以下格式:

    http://example.com/collections/collectionname/tags?view=CustomTemplate
    

    所以

    http://example.com/collections/classic/watches?view=json
    

    将为您提供所有带有手表标签的经典系列产品

    http://example.com/collections/classic/watches+home?view=json
    

    将为您提供带有手表和家居标签的经典系列中的所有产品。

    您不需要将集合硬连接到新的 json 模板中。您只需简单地请求所需的集合 - 并告诉 shopify 将其呈现为 json(使用您创建的 json 模板)。

    一旦您拥有 json 格式的产品,您就可以随心所欲地使用它们。

    【讨论】:

    • 感谢您的回复 - 假设我有 20 个不同颜色的标签,我需要在新模板中包含每个单独的标签(格式为 json)?
    • 不 - 您只需将标签附加到 url...Shopify 将从带有任何标签的集合中抓取所有产品,并使用您创建的 json 模板呈现它们。我已经更新了我的答案,并对此进行了澄清。如果它成功了,请给我打勾;)谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多