Php 列出所有变体,显示其属性链接到Woocommerce产品循环上的变体
我想为每个属性添加一个链接,以便在单击访问者时选择变体 基本上是在每个术语中添加一个类似的链接 根据答案代码,我尝试使用以下方法插入术语链接:Php 列出所有变体,显示其属性链接到Woocommerce产品循环上的变体,php,wordpress,loops,woocommerce,product-variations,Php,Wordpress,Loops,Woocommerce,Product Variations,我想为每个属性添加一个链接,以便在单击访问者时选择变体 基本上是在每个术语中添加一个类似的链接 根据答案代码,我尝试使用以下方法插入术语链接: $term_link = get_term_link( $term ); 但它没有提供指向所选产品属性的链接 非常感谢您的帮助。当您有多个属性时,指向变体的链接是来自每个属性的术语的组合……因此,您不能在术语本身上有指向特定变体的链接,因为一个术语与多个变体相关 所以你需要一些不同的东西。最好的方法是列出可变产品的所有可见变体,显示其与产品循环中的变体
$term_link = get_term_link( $term );
但它没有提供指向所选产品属性的链接
非常感谢您的帮助。当您有多个属性时,指向变体的链接是来自每个属性的术语的组合……因此,您不能在术语本身上有指向特定变体的链接,因为一个术语与多个变体相关 所以你需要一些不同的东西。最好的方法是列出可变产品的所有可见变体,显示其与产品循环中的变体链接的属性,如下所示:
add_action( 'woocommerce_after_shop_loop_item', 'product_variable_linked_variations_on_loop', 7 );
function product_variable_linked_variations_on_loop() {
global $product;
if( $product->is_type('variable') ) { // Only for variable products
$output = array(); // Initializing
// Loop through visible children Ids (variations ids)
foreach( $product->get_visible_children() as $variation_id ) {
$variation = wc_get_product($variation_id); // The varition object instance
$permalink = $variation->get_permalink(); // The link to the variation
$attributes = array(); // Initializing
// Loop through attributes
foreach( $variation->get_attributes() as $attribute => $value ) {
$attribute_label = wc_attribute_label( $attribute, $product );
$attribute_value = $variation->get_attribute($attribute);
$attributes[] = $attribute_label . ': ' . $attribute_value;
}
$output[] = '<a href="' . $permalink . '">' . implode(', ', $attributes) . '</a>';
}
echo '<div class="product-attributes">';
echo '<span>' . implode('</span><br><span>', $output) . '</span>';
echo '</div>';
}
}
add_action('woocommerce_在_shop_loop_项目之后,'product_variable_linked_variations_在_loop上,'7');
函数乘积变量链接变量循环()上的变量{
全球$产品;
如果($product->为_类型('variable')){//仅适用于可变产品
$output=array();//正在初始化
//循环查看可见的子ID(变体ID)
foreach($product->get_visible_children()作为$VARIANCE_id){
$variation=wc_get_product($variation_id);//变量对象实例
$permalink=$variation->get_permalink();//变量的链接
$attributes=array();//正在初始化
//循环遍历属性
foreach($variation->get_attributes()作为$attribute=>$value){
$attribute\u label=wc\u attribute\u label($attribute,$product);
$attribute\u value=$variation->get\u属性($attribute);
$attributes[]=$attribute_label.:'。$attribute_value;
}
$output[]='';
}
回声';
回显“”。内爆(“
”,$output)。“”;
回声';
}
}
代码进入活动子主题(或活动主题)的functions.php文件。已测试且有效。只有当您的变量产品具有一个变量属性时,这才可能,在这种情况下,您将不会使用此类代码。因此,您可以汇总变量,但不能添加指向它的链接?当您具有多个属性时,到变体的链接是来自每个属性的术语的组合…因此您不能在术语本身上有链接,这将指向特定变体,因为一个术语与多个变体相关。我理解。但是在下拉列表中预先填充这些术语是可能的。我只有一个属性“size”,我在下面回答了一些不同的问题:列出所有可见的变体,显示它们与变体链接的属性。