我目前正在使用一个脚本在我管理的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 %}
如有任何想法/帮助,将不胜感激。
一种方法是创建一个输出json的新集合模板,并使用view参数将该模板应用于json请求。
“视图”参数只允许您动态使用不同的模板来呈现内容。https://docs.shopify.com/manual/configuration/store-customization/page-specific/collections/add-view-all-to-collection-pages
下面是一个json liquid模板示例(在我的脑海中完成-您需要检查要使用哪些liquid标记):
{% 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。那么你会要求:
/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集合中已标记为黑色(#1)的产品。因为我们在追加?view=json
根据我们的请求,Shopify将使用我们创建的json模板发送这些产品*。然后,您可以随意将json随机化,并通过JavaScript将其呈现到页面上。
Shopify收藏网址的格式如下:
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格式的产品,你可以对它们做任何你喜欢的事情。
在这个论坛上也有过类似的问题,但是没有一个提供的代码起作用,也许是因为几次Woocommerce的更新。 谢谢你!
我在一家Woocommerce商店工作,该商店的产品按父类别(如品牌、颜色、大小等)组织。这些顶级类别中的每一个都有许多子类别:例如,颜色的子类别包括红色、蓝色、粉色等。 客户希望在首页和归档页的网格/循环视图中,在每个产品缩略图下方显示每个产品的品牌和尺寸(产品是鞋)。 每个产品只有一个品牌,但可能有多种尺寸。 因此,本质上,我需要知道是否有一种方法可以只显示单个产品的特定子类别的标题,如下所示
是否有WooCommerce add_操作来显示完整的产品描述,这是在主文本编辑窗口中找到的文本(不是产品简短描述) 在谷歌上快速搜索后,我只能找到添加简短描述的建议。woocommerce_template_single_excerpt。 我试图在之后添加它。 灯箱php如下所示。
所以我差点就明白了,但这封电子邮件的最后一个细节让我的头脑有点纠结。 我需要显示产品的ACF(高级自定义字段)字段(在本例中,这是一个自定义发货时间) 但只有在新订单电子邮件(处理订单和等待付款订单发送到客户端),然后新订单电子邮件到管理员。 这是我发现的主要方式:“在Woocommerce交易电子邮件中显示产品ACF字段值”提前感谢@LoicTheAztec 我还向它添加了一些条件设置(请注意,
我已从api获取数据并成功地将数据显示在市场中。现在,当用户单击一个产品时,它应该打开产品详情页并将该产品的数据发送到详情页。我想在单击该产品时将一个产品的特定数据从Api传递到产品详情页。请帮助 这是呈现各种产品的代码 如何将单击的链接的id传递到详细信息页面
似乎WooCommerce产品的搜索功能没有检查“产品标签”分类术语,也没有检查SKU字段?我将SKU作为产品标签添加到它们各自的产品中,但当我搜索SKU时,它仍然没有返回任何内容。。。。如何使搜索功能检查产品标签术语?我尝试了很多方法,从添加tax\u查询到pre\u get\u post过滤器,再到一个全新的WP\u查询循环,由于某种原因,它无法搜索产品标签。。。。那么,产品标签的意义是什么呢
我在WooCommerce单一产品页面上创建了六个自定义属性,其中包含一个自定义函数。它们包括:图标、标签和术语。 基于用每个答案代码的自定义图像替换WooCommerce属性标签,我使用了以下函数(使用自定义HTML标记,与WooCommerce product-attributes.php模板中的标记不同): 该函数工作正常,但存在一个问题:如果特定属性的项不止一个(例如颜色:红色、蓝色、绿色