With wc_get_products()
you will get an array of WC_Product
Objects.
Get product properties
Each WC_Product
Object is a CRUD object with protected data. That data is accessible using related WC_Product
and WC_Data
methods.
For custom meta data (or custom fields)
, you will use the WC_Data
get_meta()
method on your specific meta_keys
that you can find in wp_postmeta
database table for a product ID (post_id
). So for example if the custom meta_key
is _my_image_url
you will get the value from the WC_Product
Object using:
$value = $product->get_meta('_my_image_url');
Below is a more complete example, using everything:
// The WC_Product_Query
$products = wc_get_products('limit' => 10);
// Loop though `WC_Product` Objects
foreach ( $products as $product ) {
$product_id = $product->get_id(); // The product ID
$product_name = $product->get_name(); // The product name
$product_sku = $product->get_sku(); // The product SKU
$product_price = $product->get_price(); // The product price
$image_url = wp_get_attachment_image_src( $product->get_image_id() ); // The product image URL
## ----- Custom meta data ----- ##
$custom_value = $product->get_meta('_custom_key'); // Custsom meta data
}
You can also use get_post_meta()
WordPress function from the product Id (or the post ID) like:
$product_name = get_the_title($product_id); // The product name
$product_sku = get_post_meta($product_id, '_sku', true); // The product SKU
$product_price = get_post_meta($product_id, '_price', true); // The product price
$image_url = wp_get_attachment_image_src( get_post_meta($product_id, '_thumbnail_id', true) ); // The product image URL
## ----- Custom meta data ----- ##
$custom_value = get_post_meta($product_id, '_custom_key', true); // Custsom meta
Updating product properties
Now you can also use any available setter method to change product properties values and you will use at the end save();
method to save data and refresh cached data.
Transfer to a custom DB table
To transfer some data in a custom table, you will use WPDB
WordPress class that allow you to make any SQL query to WordPress custom tables.