Skip to content

Instantly share code, notes, and snippets.

@greenhornet79
Created October 13, 2015 13:31
Show Gist options
  • Save greenhornet79/6f6c15d1cc83e238b1d0 to your computer and use it in GitHub Desktop.
Save greenhornet79/6f6c15d1cc83e238b1d0 to your computer and use it in GitHub Desktop.
<?php
function generate_stock_report_csv() {
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
// set file name with current date
header('Content-Disposition: attachment; filename=endo-stock-report-' . date('Y-m-d') . '.csv');
// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// set the column headers for the csv
$headings = array( 'Product', 'Stock' );
// output the column headings
fputcsv($output, $headings );
// get all simple products where stock is managed
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_manage_stock',
'value' => 'yes'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => array('simple'),
'operator' => 'IN'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$row = array( $product->get_title(), $product->stock );
fputcsv($output, $row);
endwhile;
// get all product variations where stock is managed
$args = array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_stock',
'value' => array('', false, null),
'compare' => 'NOT IN'
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product = new WC_Product_Variation( $loop->post->ID );
$row = array( $product->get_title() . ', ' . get_the_title( $product->variation_id ), $product->stock );
fputcsv($output, $row);
endwhile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment