Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active February 15, 2024 19:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinsays/f441f28a35cf55d1ca02f7713ee2077b to your computer and use it in GitHub Desktop.
Save devinsays/f441f28a35cf55d1ca02f7713ee2077b to your computer and use it in GitHub Desktop.
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