Echo-specific Product Attributes and Metadata in the WooCommerce Basket

Update (related to author comments):

I would like to configure WooCommerce cart.php to display some metadata that works great on the product page using the Essential Grid Premium Plugin .

I would like to display some product attribute fields, as well as some custom meta fields that I created using the Essential Grid meta field creator .

For testing, I use the attribute 'Height' (therefore 'pa_height' ) and the custom field 'Age' , which has bullets of 'eg-age-cal' .

Currently, I have tried using the following:

 <?php echo get_post_meta($product_id, 'pa_height', true );?> 

As well as:

 <?php echo get_post_meta($product_id, 'eg-age-cal', true );?> 

But that does not work.

I managed to get the code to work using:

 <?php echo get_post_meta($product_id, '_regular_price', true );?> 

So, I know the code is working.

I just need some help in developing how I can get the values ​​from this custom attribute and custom field.

Thanks.

+6
source share
1 answer

Update (WC 3+ compatible)

Following your explanation in your comment below, I just found that you are using the Premium Essential Grid premium plugin (commercial plugin) to create some custom fields and attributes related to your wooCommerce products.

At this moment I can’t help , because I have never used this plugin before, and I don’t know where the data in this plugin is stored in the database.

I think that you cannot use the usual WordPress / WooCommerce functions to get this data, and for this very reason you will not get any data using get_post_meta() , as usual ...

The best way to get help:
- search / explore your database for custom field data.
- for search / query in Essential Grid plugin authors support streams.


My original answer:

For the attributes defined in your products , using the get_post_meta() function with the $product_id variable, you need to use it this way to get the required data (this is an array of values):

 // getting the defined product attributes $product_attr = get_post_meta( $product_id, '_product_attributes' ); // displaying the array of values (just to test and to see output) echo var_dump( $product_attr ); 

You can also use the get_attributes() function (more recommended), as follows:

 // Creating an object instance of the product $_product = new WC_Product( $product_id ); // getting the defined product attributes $product_attr = $_product->get_attributes(); // displaying the array of values (just to test and to see output) echo var_dump( $product_attr ); 

All code is tested and working.

NOW DATA CARDS ARE INSTALLED IN CHAINS AND SESSIONS , and you will need to use the WC()->cart syntax to get the data and cart items

Thus, you can use this code to get items (products) in the basket:

 foreach ( WC()->cart->get_cart() as $cart_item ) { $product = $cart_item['data']; if(!empty($product)){ // getting the defined product attributes $product_attr = $_product->get_attributes(); // displaying the attributes array of values (just to test and to see output) echo var_dump( $product_attr ) . '<br>'; } } 

This will display an array of value attributes for each product in the CART.


A solution based on this thread using wc_get_product_terms() inside the same code snippet to get your attribute:

 foreach ( WC()->cart->get_cart() as $cart_item ) { $product = $cart_item['data']; if(!empty($product)){ // compatibility with WC +3 $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; // Getting "height" product attribute $myAttribute = array_shift( wc_get_product_terms( $product_id, 'pa_height', array( 'fields' => 'names' ) ) ); echo $myAttribute . '<br>'; } } 

References:

+4
source

All Articles