kodfire
kodfire

Reputation: 1783

How to get product specific meta data in WooCommerce

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 need price and color (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'
    .
    .
    .
)

Upvotes: 5

Views: 33485

Answers (3)

LoicTheAztec
LoicTheAztec

Reputation: 254373

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_meta() 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 …

Upvotes: 11

Max
Max

Reputation: 136

Here is Woocommerce Code Reference about how to get metadata from each product.

Also, this is example how to get metadata by key below:

function woo_custom_additional_information_tab_content() {

global $product;

$product_data = $product->get_meta('_specifications');

echo $product_data;

}

Upvotes: 2

Naveen Pal
Naveen Pal

Reputation: 64

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.

Upvotes: 1

Related Questions