Last active
February 15, 2024 19:39
-
-
Save devinsays/f441f28a35cf55d1ca02f7713ee2077b to your computer and use it in GitHub Desktop.
WooCommerce Export Product Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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