在 Woocommerce 中获取特定产品类别的特定产品属性值
Get specific product attribute values for a specific product category in Woocommerce
我已经用这段代码获取了所有的颜色属性:
$color_terms = get_terms(
array(
'taxonmy'=>'pa_color'
));
这适用于商店页面和 returns 所有颜色,但我如何将其限制在特定类别?假设在名为 "Shirts" 的类别中,我只有 2 种颜色,我只想显示这 2 种颜色。
尝试以下使用独特灯光 SQL 查询的自定义函数:
function get_attribute_terms_in_product_cat( $category_term_name, $attribute_taxonomy ){
global $wpdb;
$terms = $wpdb->get_results( "SELECT DISTINCT t.*
FROM {$wpdb->prefix}terms as t
JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_id = t.term_id
JOIN {$wpdb->prefix}term_relationships as tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tt.taxonomy LIKE '$attribute_taxonomy'
AND tr.object_id IN ( SELECT DISTINCT tr2.object_id
FROM {$wpdb->prefix}term_relationships as tr2
JOIN {$wpdb->prefix}term_taxonomy as tt2 ON tt2.term_taxonomy_id = tr2.term_taxonomy_id
JOIN {$wpdb->prefix}terms as t2 ON tt2.term_id = t2.term_id
WHERE tt2.taxonomy LIKE 'product_cat' AND t2.name = '$category_term_name'
)" );
return $terms;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
用法:
// Get the "pa_color" attribute terms for product category "Shirts"
$terms = get_attribute_terms_in_product_cat( "Shirts", "pa_color" );
这里"Shirts"是产品类别术语名称,"pa_color"是产品属性分类…
您将获得一组产品属性值(术语对象),其中包含:
- 术语 ID (关键字
term_id
)
- 术语名称(关键字
name
)
- 术语 slug (key
slug
)
您可以使用 foreach 循环访问每个术语对象:
// Get the "pa_color" attribute terms for product category "Shirts"
$terms = get_attribute_terms_in_product_cat( "Shirts", "pa_color" );
// Loop through each term
foreach( $terms as $term ){
$term_id = $term->term_id;
$term_name = $term->name;
$term_slug = $term->slug;
}
我已经用这段代码获取了所有的颜色属性:
$color_terms = get_terms(
array(
'taxonmy'=>'pa_color'
));
这适用于商店页面和 returns 所有颜色,但我如何将其限制在特定类别?假设在名为 "Shirts" 的类别中,我只有 2 种颜色,我只想显示这 2 种颜色。
尝试以下使用独特灯光 SQL 查询的自定义函数:
function get_attribute_terms_in_product_cat( $category_term_name, $attribute_taxonomy ){
global $wpdb;
$terms = $wpdb->get_results( "SELECT DISTINCT t.*
FROM {$wpdb->prefix}terms as t
JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_id = t.term_id
JOIN {$wpdb->prefix}term_relationships as tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tt.taxonomy LIKE '$attribute_taxonomy'
AND tr.object_id IN ( SELECT DISTINCT tr2.object_id
FROM {$wpdb->prefix}term_relationships as tr2
JOIN {$wpdb->prefix}term_taxonomy as tt2 ON tt2.term_taxonomy_id = tr2.term_taxonomy_id
JOIN {$wpdb->prefix}terms as t2 ON tt2.term_id = t2.term_id
WHERE tt2.taxonomy LIKE 'product_cat' AND t2.name = '$category_term_name'
)" );
return $terms;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
用法:
// Get the "pa_color" attribute terms for product category "Shirts"
$terms = get_attribute_terms_in_product_cat( "Shirts", "pa_color" );
这里"Shirts"是产品类别术语名称,"pa_color"是产品属性分类…
您将获得一组产品属性值(术语对象),其中包含:
- 术语 ID (关键字
term_id
) - 术语名称(关键字
name
) - 术语 slug (key
slug
)
您可以使用 foreach 循环访问每个术语对象:
// Get the "pa_color" attribute terms for product category "Shirts"
$terms = get_attribute_terms_in_product_cat( "Shirts", "pa_color" );
// Loop through each term
foreach( $terms as $term ){
$term_id = $term->term_id;
$term_name = $term->name;
$term_slug = $term->slug;
}