Home Woocommerce How to get Woocommerce Product Attributes names and value?

How to get Woocommerce Product Attributes names and value?

by therichpost
2 comments
woocommerce

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to get woocommerce product attributes names and value?

Woocommerce is know for the best e-commerce wordpress plugin and User can easily add and sell his/her products easily.

Woocommerce has great ability to add extra features or I can extra attributes of any product and I will show the code to get that attributes.



If you are new in Woocommerce then you can check my old posts related to Woocommerce.


1. Here is the code for How to get Woocommerce Product Attributes names and value? and you can add this code anywhere into your wordpress theme’s template file and I am doing to this for single product but we can also use this code in wp query for product:

<?php $product_attr = get_post_meta( get_the_ID(), '_product_attributes' ); ?>
<table>
 <tbody>
      <?php 
          foreach ($product_attr as $attr) {
              foreach ($attr as $attribute) { ?>
                <tr>
                   <th><?php $attrnames = str_replace("pa_", "", $attribute['name']);
                              echo str_replace("-", " ", $attrnames);
                     ?></th>
                    <td><?php $attrvalue = array( wc_get_product_terms( get_the_ID(), $attribute['name'], array( 'fields' => 'names' ) ) );
                        $attrvalues = implode(",", $attrvalue[0]);
                        echo $attrvalues;
                     ?></td>
                </tr>
      <?php } } ?>
 </tbody>
</table>

 

If you have any query related to this post, then please let me know.

Jassa Jatt,

Thank you

You may also like

2 comments

Morteza September 20, 2022 - 9:28 pm

Hi Jassa Jatt!
Thanks a lot for your nice codes…
You can use “wc_attribute_label” for display name of the attribute label:

Thanks again

Reply
therichpost September 21, 2022 - 4:45 pm

You are most welcome 🙂

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.