php - Output an array of terms names for WooCommerce product attributes -
this code , need display names of each array:
black
blue
green
foreach( $product->get_variation_attributes() $taxonomy => $terms_slug ){ // taxonomy object $taxonomy_obj = get_taxonomy( $taxonomy ); $taxonomy_name = $taxonomy_obj->name; // name (we got it) $taxonomy_label = $taxonomy_obj->label; // label // setting data in array $variations_attributes_and_values[$taxonomy] = array('label' => $taxonomy_obj->label); foreach($terms_slug $term){ // getting term object slug $term_obj = get_term_by('slug', $term, $taxonomy); $term_id = $term_obj->term_id; // id <== <== <== <== <== <== here $term_name = $term_obj->name; // name $term_slug = $term_obj->slug; // slug $term_name = $term_obj->description; // description // setting terms id , values in array $variations_attributes_and_values[$taxonomy]['terms'][$term_obj->term_id] = array( 'name' => $term_obj->name, 'slug' => $term_obj->slug ); }} and arrays:
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 ) ) )) how can it?
/////new edit
echo '<pre>'; print_r($term_obj->name);echo '</pre>'; i used code showing names display last name!
this 1 of aswer's code: get product variations attributes values term id , name
it has been done before woocommerce 3 release.
since woocommerce 3+ things have changed little bit. don't need code in function. here arranged version feet needs.
remember can have many attributes variable product, need use
foreachloops output separated values each product attribute…
here code:
foreach( $product->get_variation_attributes() $taxonomy => $terms_slug ){ // attribute label (in woocommerce 3+) $taxonomy_label = wc_attribute_label( $taxonomy, $product ); foreach($terms_slug $term){ // getting term object slug $term_name = get_term_by('slug', $term, $taxonomy)->name; // setting terms id , values in array $attributes_and_terms_names[$taxonomy_label][$term] = $term_name; } } echo '<pre>'; print_r($attributes_and_terms_names); echo '</pre>'; then get:
array ( [color] => array ( [0] => black [1] => green [2] => red ) ) usage example output:
foreach ( $attributes_and_terms_names $attribute_name => $terms_name ){ // related attribute term names in coma separated string $terms_string = implode( ', ', $terms_name ); echo '<p>' . $attribute_name . ': ' . $terms_string . '</p>'; } you get:
color: black, green, red
Comments
Post a Comment