How to get product specific meta data in WooCommerce
Solution 1
There is multiple ways depending on what you want:
1). You will need to use WC_Product
methods on the WC_Product Object to get product properties:
$product_id = 37; // Defined product Id for testing
// Get the WC_Product Object from the product ID (optional)
$product = wc_get_product($product_id); // If needed
// Get product active price
$product_data = $product->get_price();
// … and so on …
2). For specific or custom product meta data, you will use WC_Data
method get_data()
using the desired meta key like:
$product_id = 37; // Defined product Id for testing
// Get the WC_Product Object from the product ID (optional)
$product = wc_get_product($product_id); // If needed
// Get custom meta data froma specific meta key
$product_data = $product->get_meta('my_meta_key');
// … and so on …
3). You can use instead the WC_Data
method get_data()
on the WC_Product
object to get unprotected meta data in an array:
$product_id = 37; // Defined product Id for testing
// Get the WC_Product Object from the product ID (optional)
$product = wc_get_product($product_id); // If needed
// Get product unprotected meta data in an array
$product_data = $product->get_data();
// Output row data
echo '<pre>'. print_r( $obj, true ) . '</pre>';
You will get something like (extract):
Array
(
[id] => 37
[name] => Happy Ninja Dish Hip
[slug] => happy-ninja-dish
[date_created] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2015-12-26 11:53:15.000000
[timezone_type] => 3
[timezone] => Europe/Paris
)
[date_modified] => WC_DateTime Object
(
[utc_offset:protected] => 0
[date] => 2020-07-09 19:25:32.000000
[timezone_type] => 3
[timezone] => Europe/Paris
)
[status] => publish
[featured] =>
[catalog_visibility] => visible
[description] => Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet…
[add_to_cart id="53"]
[short_description] => Pellentesque habitant morbi tristique senectus...
[sku] => Gh2563
[price] => 90.50
[regular_price] => 100
[sale_price] => 90.50
// … and so on …
Solution 2
There can be two ways to do this, One is to filter the metadata array manually in the foreach loop and other is to hit a custom query to wp_postmeta table for specific keys.
Related videos on Youtube
kodfire
Updated on May 26, 2022Comments
-
kodfire almost 2 years
I'm using WooCommerce and want to get the meta keys specifically for the product e.g. price, color and so on... But the problem is that I don't want to get all meta keys but only the ones needed. For example I don't need
_edit_last
,_edit_lock
,_wc_review_count
... and just needprice
andcolor
(as an example). If it was a web page that wasn't important but I don't want to show them on web page but I want to send them as json to somewhere else. So the data should be filtered. BTW it is in plugin and I want to share it with others so I can't/shouldn't access their WooCommerce api.my code that gets all meta keys (but I want only some of them):
$attributes = get_post_meta($product->get_id()); foreach($attributes as $key => $value) { $result['products'][$p]['attributes'][$key] = $value[0]; }
and the result of code above is:
[attributes] => Array ( [_edit_last] => 1 [_edit_lock] => 1594817821:1 . . . )
but I want:
[attributes] => Array ( [price] => 1000 [color] => 'red' . . . )