Skip to content
Advertisement

Save CSV Export to Server

I have the code below which triggers when a button is pressed in the WordPress admin panel. This opens a csv file called ‘ss-stock-export.csv’.

How can I save this file to my server in my uploads directory at wp-content/uploads/exports/? I tried to use file_put_contents but that doesn’t seem to be working right. The CSV file appears in the right spot but it is blank. Possibly something wrong with my $output?

<?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=ss-stock-export.csv');
    // create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');
    // set the column headers for the csv
    $headings = array( 'sku', 'qty', 'is_in_stock' );
    // output the column headings
    fputcsv($output, $headings );
    // get all simple products where stock is managed

    // 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 );
        $ss_sku = get_post_meta( $product->variation_id, 'ss_sku', true);
        $stock = $product->stock;
        settype($stock, "integer");
        if ($stock > 0){ $stock_status = 1;} else {$stock_status = 0;}
        $row = array( $ss_sku , $product->stock, $stock_status );
        fputcsv($output, $row);
    endwhile;
    $filename = "ss-stock-export.csv"; // Trying to save file in server
    file_put_contents(ABSPATH . "wp-content/uploads/exports/" . $filename, $output);
} ?>

Advertisement

Answer

This did the trick

$csv = "sku,qty,is_in_stock n";//Column headers
$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 );
    $ss_sku = get_post_meta( $product->variation_id, 'ss_sku', true);
    $stock = $product->stock;
    settype($stock, "integer");
    if ($stock > 0){ $stock_status = 1;} else {$stock_status = 0;}
    $row = array( $ss_sku , $product->stock, $stock_status );
    $row_array= array($row);
        foreach ($row_array as $record){
        $csv.= $record[0].','.$record[1].','.$record[2]."n"; //Append data to csv
        }
endwhile;
$csv_handler = fopen ('ss-stock-export.csv','w');
fwrite ($csv_handler,$csv);
fclose ($csv_handler);
file_put_contents(ABSPATH . "wp-content/uploads/exports/ss-stock-export.csv", $csv);       
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement