Download

<?php

//balans
//add button (with styling) to orders overview backend
add_filter( 'woocommerce_admin_order_actions', 'add_download_link_button', 100, 2 );
function add_download_link_button( $actions, $the_order ) {
    if ( ! $the_order->has_status( array( 'cancelled' ) ) ) {
        $actions['download'] = array(
            'url'       => admin_url( 'admin-ajax.php?action=downloadlabel&order_id=' . $the_order->id ),
            'name'      => __( 'Download', 'woocommerce' ),
            'action'    => "view download",
        );
    }
    return $actions;
}

add_action( 'wp_ajax_downloadlabel', 'custom_download_csv' );
function custom_download_csv() {
  setlocale(LC_MONETARY, 'nl_NL');
  if(isset($_GET['order_id'])) {
    header('Content-Type: application/excel');
    header('Content-Disposition: attachment; filename="order_'.$_GET['order_id'].'.csv"');
    $order = new WC_Order( $_GET['order_id'] );
    $order_meta = get_post_meta($_GET['order_id']);
    $items = $order->get_items();
    $downloadCSV = array();
    //$downloadCSV[] = array('Klantadres', 'Productnr', 'Nummer', 'Label', 'Datum', 'Type', 'Eigenschappen', 'Reparatie', 'Reparatieomschrijving', 'Naam Eigenaar');
    foreach ($items as $key => $product ) {
      $productMeta = $product['item_meta'];
      //echo '<pre>';print_r($productMeta);echo '</pre>';
      //die();exit();
      for($i = 0; $i < $product['qty']; $i++) {
        $downloadCSV[] = array(
          /*$_GET['order_id'],
          $order->order_date,
          $order_meta['_billing_first_name'][0].' '.$order_meta['_billing_last_name'][0],
          $order->customer_message,
          $order_meta['_billing_email'][0].', '.$order_meta['_billing_phone'][0],*/
          /*1*/ 1,
          /*2*/ $order_meta['_billing_country'][0].', '.$order_meta['_billing_city'][0].'. '.$order_meta['_billing_address_1'][0].$order_meta['_billing_address_2'][0],
          /*3*/ $product['product_id'],
          /*4*/ ($productMeta['Labelnummer'][0] ? $productMeta['Labelnummer'][0] : unserialize($product['labels'])[$i]),
          /*5*/ date('d-m-Y'),
          /*6*/ strstr($productMeta['Welk wasgoed wil je aanbieden?'][0], ' ', true) ?: $productMeta['Welk wasgoed wil je aanbieden?'][0],
          /*7*/ strstr($productMeta['Extra behandeling'][0], ' ', true) ?: $productMeta['Extra behandeling'][0],
          /*8*/ ($productMeta['Reparatie gewenst?'][0] ? strstr($productMeta['Reparatie gewenst?'][0], ' ', true) ?: $productMeta['Reparatie gewenst?'][0] : 'Geen'),
          /*9*/ ($productMeta['Reparatieomschrijving'][0] ? trim(preg_replace('/\s\s+/', ' ', strip_tags($productMeta['Reparatieomschrijving'][0]) )) : 'Geen reparatieomschrijving') ,
          /*10*/ //($productMeta['Naam eigenaar'] ? $productMeta['Naam eigenaar'][0] : $order_meta['_billing_first_name'][0].' '.$order_meta['_billing_last_name'][0]),
          /*10*/ ($productMeta['Naam eigenaar'] ? $productMeta['Naam eigenaar'][0].', ' : '').$order_meta['_billing_first_name'][0].' '.$order_meta['_billing_last_name'][0],
          /*11*/ $order_meta['_billing_first_name'][0].' '.$order_meta['_billing_last_name'][0],
          /*12*/ explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Welk wasgoed wil je aanbieden?'][0])[0],
          /*13*/ explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Extra behandeling'][0])[0],
          /*14*/ trim(preg_replace('/\s\s+/', ' ', strip_tags( explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Reparatieomschrijving'][0])[0] ))),
          /*15*/ money_format('%+n', $productMeta['_line_total'][0] + $productMeta['_line_tax'][0]),
          /* 16 = 12 */ money_format('%+n', explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Welk wasgoed wil je aanbieden?'][0])[1]),
          /* 17 = 13 */ money_format('%+n', explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Extra behandeling'][0])[1]),
          /* 18 = 14 */ ($productMeta['Reparatieomschrijving'][0] ? money_format('%+n', explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Reparatieomschrijving'][0])[1]) : '')
          /* 19 = 15 */ //money_format('%+n', $productMeta['_line_total'][0] + $productMeta['_line_tax'][0])
        );
      }
      //echo $i;
    }
    //print_r($downloadCSV);
    //exit();
    //die();

    $fp = fopen('php://output', 'w');
    foreach ($downloadCSV as $line) {
      echo implode(';', $line)."\n";
    }
    exit();
    die();
    /*foreach ($downloadCSV as $line) {
      fputcsv($fp, $line, ';');
    }
    fclose($fp);*/
  }
}
?>
Last Updated:
Contributors: Niek Vlam, Suite Seven