【问题标题】:Getting all the product attributes and their terms in WooCommerce在 WooCommerce 中获取所有产品属性及其条款
【发布时间】:2020-11-22 00:32:06
【问题描述】:
我已经研究了一整天,但似乎无法得到一个直接的答案我如何才能获得设置的产品属性以及每个配置的条款?
这就是我现在拥有的
//get the terms
$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();
if ( $attribute_taxonomies ) {
foreach ($attribute_taxonomies as $tax) {
//dont know what to add here
}
}
var_dump($taxonomy_terms);
【问题讨论】:
标签:
php
wordpress
woocommerce
custom-taxonomy
taxonomy-terms
【解决方案1】:
您将在下面获得所有产品属性及其各自术语名称的列表:
echo '<ul>';
// Loop through WooCommerce registered product attributes
foreach( wc_get_attribute_taxonomies() as $values ) {
// Get the array of term names for each product attribute
$term_names = get_terms( array('taxonomy' => 'pa_' . $values->attribute_name, 'fields' => 'names' ) );
echo '<li><strong>' . $values->attribute_label . '</strong>: ' . implode(', ', $term_names);
}
echo '</ul>';
如果您希望获取 WP_terms 对象的数组,您将使用:
// Get the array of the WP_Terms Object for the each product attribute
$terms = get_terms( array('taxonomy' => 'pa_' . $values->attribute_name );
这将允许使用 foreach 循环从每个术语中获取您想要的内容……
【解决方案2】:
首先用 var_dump() 检查 wc_get_attribute_taxonomies() 函数。