输出 WooCommerce 产品属性的术语名称数组
Output an array of terms names for WooCommerce product attributes
这是我的代码,我需要显示每个数组的名称:
黑色
蓝色
绿色
foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ){
// To get the taxonomy object
$taxonomy_obj = get_taxonomy( $taxonomy );
$taxonomy_name = $taxonomy_obj->name; // Name (we already got it)
$taxonomy_label = $taxonomy_obj->label; // Label
// Setting some data in an array
$variations_attributes_and_values[$taxonomy] = array('label' => $taxonomy_obj->label);
foreach($terms_slug as $term){
// Getting the term object from the slug
$term_obj = get_term_by('slug', $term, $taxonomy);
$term_id = $term_obj->term_id; // The ID <== <== <== <== <== <== HERE
$term_name = $term_obj->name; // The Name
$term_slug = $term_obj->slug; // The Slug
$term_name = $term_obj->description; // The Description
// Setting the terms ID and values in the array
$variations_attributes_and_values[$taxonomy]['terms'][$term_obj->term_id] = array(
'name' => $term_obj->name,
'slug' => $term_obj->slug
);
}}
这是我的数组:
Array(
[pa_color] => Array(
[label] => Color
[terms] => Array(
[8] => Array(
[name] => Black
[slug] => black'
)
[9] => Array(
[name] => Blue
[slug] => blue
)
[11] => Array(
[name] => Green
[slug] => green
)
)
))
我该怎么做?
/////新编辑
echo '<pre>'; print_r($term_obj->name);echo '</pre>';
我使用此代码显示姓名,但只显示姓氏!
这是我的 aswer 代码之一:
在 Woocommerce 3 发布之前就已经完成了。
自从 woocommerce 3+ 以来,情况发生了一些变化。您不需要函数中的所有这些代码。所以这里有一个安排版本来满足您的需求。
Remember that you can have many attributes for a variable product, so you will need to use foreach
loops to output separated values for each product attribute…
代码如下:
foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ){
// To get the attribute label (in WooCommerce 3+)
$taxonomy_label = wc_attribute_label( $taxonomy, $product );
foreach($terms_slug as $term){
// Getting the term object from the slug
$term_name = get_term_by('slug', $term, $taxonomy)->name;
// Setting the terms ID and values in the array
$attributes_and_terms_names[$taxonomy_label][$term] = $term_name;
}
}
echo '<pre>'; print_r($attributes_and_terms_names); echo '</pre>';
那么您将获得:
Array
(
[Color] => Array
(
[0] => Black
[1] => Green
[2] => Red
)
)
使用示例输出:
foreach ( $attributes_and_terms_names as $attribute_name => $terms_name ){
// get the related attribute term names in a coma separated string
$terms_string = implode( ', ', $terms_name );
echo '<p>' . $attribute_name . ': ' . $terms_string . '</p>';
}
您将获得:
Color: Black, Green, Red
这是我的代码,我需要显示每个数组的名称:
黑色
蓝色
绿色
foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ){
// To get the taxonomy object
$taxonomy_obj = get_taxonomy( $taxonomy );
$taxonomy_name = $taxonomy_obj->name; // Name (we already got it)
$taxonomy_label = $taxonomy_obj->label; // Label
// Setting some data in an array
$variations_attributes_and_values[$taxonomy] = array('label' => $taxonomy_obj->label);
foreach($terms_slug as $term){
// Getting the term object from the slug
$term_obj = get_term_by('slug', $term, $taxonomy);
$term_id = $term_obj->term_id; // The ID <== <== <== <== <== <== HERE
$term_name = $term_obj->name; // The Name
$term_slug = $term_obj->slug; // The Slug
$term_name = $term_obj->description; // The Description
// Setting the terms ID and values in the array
$variations_attributes_and_values[$taxonomy]['terms'][$term_obj->term_id] = array(
'name' => $term_obj->name,
'slug' => $term_obj->slug
);
}}
这是我的数组:
Array(
[pa_color] => Array(
[label] => Color
[terms] => Array(
[8] => Array(
[name] => Black
[slug] => black'
)
[9] => Array(
[name] => Blue
[slug] => blue
)
[11] => Array(
[name] => Green
[slug] => green
)
)
))
我该怎么做?
/////新编辑
echo '<pre>'; print_r($term_obj->name);echo '</pre>';
我使用此代码显示姓名,但只显示姓氏!
这是我的 aswer 代码之一:
在 Woocommerce 3 发布之前就已经完成了。
自从 woocommerce 3+ 以来,情况发生了一些变化。您不需要函数中的所有这些代码。所以这里有一个安排版本来满足您的需求。
Remember that you can have many attributes for a variable product, so you will need to use
foreach
loops to output separated values for each product attribute…
代码如下:
foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ){
// To get the attribute label (in WooCommerce 3+)
$taxonomy_label = wc_attribute_label( $taxonomy, $product );
foreach($terms_slug as $term){
// Getting the term object from the slug
$term_name = get_term_by('slug', $term, $taxonomy)->name;
// Setting the terms ID and values in the array
$attributes_and_terms_names[$taxonomy_label][$term] = $term_name;
}
}
echo '<pre>'; print_r($attributes_and_terms_names); echo '</pre>';
那么您将获得:
Array
(
[Color] => Array
(
[0] => Black
[1] => Green
[2] => Red
)
)
使用示例输出:
foreach ( $attributes_and_terms_names as $attribute_name => $terms_name ){
// get the related attribute term names in a coma separated string
$terms_string = implode( ', ', $terms_name );
echo '<p>' . $attribute_name . ': ' . $terms_string . '</p>';
}
您将获得:
Color: Black, Green, Red