Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce Export Product Data
<?php
/**
* Exports product data.
*
* More about WP CLI scripts:
* https://wptheming.com/2021/05/wp-cli-scripts-and-woocommerce/
*
* wp eval-file export-product-data.php
*/
$products = get_posts([
'post_type' => [ 'product' ],
'posts_per_page' => -1,
'fields' => 'ids',
]);
$headers = [ 'title', 'price' ];
log_data( $headers );
foreach ( $products as $product_id ) {
$data = [];
$product = wc_get_product( $product_id );
if ( $product->get_stock_status() === 'outofstock' ) {
continue;
}
WP_CLI::log( "Processing: $product_id" );
$data[] = $product->get_title();
$data[] = $product->get_price();
log_data( $data );
}
function log_data( $output ) {
$file_name = 'export-data.csv';
$file_resource = fopen( $file_name, 'a' );
fputcsv( $file_resource, $output );
fclose( $file_resource );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment