Выведите массив имен терминов для атрибутов продукта WooCommerce

Это мой код, и мне нужно отобразить имена каждого моего массива:

черный

синий

зеленый

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>';

Я использовал этот код для отображения имен, но только для отображения фамилии!

0

Решение

Это один из моих кодов aswer: Получите значения атрибутов вариантов продукта, идентификатор и имя термина

Это было сделано до выхода Woocommerce 3.

Со времен woocommerce 3+ ситуация немного изменилась. Вам не нужен весь этот код в функции. Так что вот организованная версия, чтобы удовлетворить ваши потребности.

Помните что вы можете иметь много атрибутов для переменного продукта, поэтому вам нужно будет использовать foreach Циклы для вывода отдельных значений для каждого атрибута продукта…

Вот код:

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>';
}

Ты получишь:

Цвет: черный, зеленый, красный

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector