Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 输出WooCommerce产品属性的术语名称数组_Php_Wordpress_Woocommerce_Attributes_Product - Fatal编程技术网

Php 输出WooCommerce产品属性的术语名称数组

Php 输出WooCommerce产品属性的术语名称数组,php,wordpress,woocommerce,attributes,product,Php,Wordpress,Woocommerce,Attributes,Product,这是我的代码,我需要显示每个数组的名称: 黑色 蓝色 绿色 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 go

这是我的代码,我需要显示每个数组的名称:

黑色

蓝色

绿色

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
    );
}}
我怎么做

///NEW EDIT

echo '<pre>'; print_r($term_obj->name);echo '</pre>';
echo';打印(术语对象->名称);回声';

我使用此代码显示姓名,但只显示姓氏

这是我的aswer代码之一:

这是在Woocommerce 3发布之前完成的

自从woocommerce 3+以来,情况发生了一些变化。函数中不需要所有这些代码。因此,这里有一个安排好的版本来满足您的需求

记住一个可变产品可以有许多属性,因此需要使用
foreach
循环为每个产品属性输出单独的值

代码如下:

Array
(
    [Color] => Array
        (
            [0] => Black
            [1] => Green
            [2] => Red
        )
)
使用示例输出:

foreach($attributes\u和\u terms\u名称为$attribute\u name=>$terms\u name){
//获取分隔字符串中的相关属性术语名称
$terms\u string=内爆(“,”,$terms\u name);
回显“”.$attribute_name.”:“.$terms_string.”

”; }
您将获得:

颜色:黑色、绿色、红色


为什么要创建这样的嵌套数组。为什么不这样创建数组呢:-
array([terms]=>array([8]=>array([name]=>Black[slug]=>Black')[9]=>array([name]=>Blue[slug]=>Blue)[11]=>array([name]=>Green[slug]=>Green)))
您的
获取术语的功能是什么?这给了你
$term\u obj
,但我们看不到它是如何初始化的。这是为了商业!在这种情况下,您应该编辑问题并添加相关标签。
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>';
}