WoocommerceWordpressWordpress Tricks

How to get Woocommerce Product Attributes names and value?

woocommerce
346Views

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

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

Positive SSL