Get the solution ↓↓↓
There is multiple ways depending on what you want:
1). You will need to use 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 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 on theWC_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 …
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;
}
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.