<?php
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();
foreach ($items as $key => $product ) {
$productMeta = $product['item_meta'];
for($i = 0; $i < $product['qty']; $i++) {
$downloadCSV[] = array(
1,
$order_meta['_billing_country'][0].', '.$order_meta['_billing_city'][0].'. '.$order_meta['_billing_address_1'][0].$order_meta['_billing_address_2'][0],
$product['product_id'],
($productMeta['Labelnummer'][0] ? $productMeta['Labelnummer'][0] : unserialize($product['labels'])[$i]),
date('d-m-Y'),
strstr($productMeta['Welk wasgoed wil je aanbieden?'][0], ' ', true) ?: $productMeta['Welk wasgoed wil je aanbieden?'][0],
strstr($productMeta['Extra behandeling'][0], ' ', true) ?: $productMeta['Extra behandeling'][0],
($productMeta['Reparatie gewenst?'][0] ? strstr($productMeta['Reparatie gewenst?'][0], ' ', true) ?: $productMeta['Reparatie gewenst?'][0] : 'Geen'),
($productMeta['Reparatieomschrijving'][0] ? trim(preg_replace('/\s\s+/', ' ', strip_tags($productMeta['Reparatieomschrijving'][0]) )) : 'Geen reparatieomschrijving') ,
($productMeta['Naam eigenaar'] ? $productMeta['Naam eigenaar'][0].', ' : '').$order_meta['_billing_first_name'][0].' '.$order_meta['_billing_last_name'][0],
$order_meta['_billing_first_name'][0].' '.$order_meta['_billing_last_name'][0],
explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Welk wasgoed wil je aanbieden?'][0])[0],
explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Extra behandeling'][0])[0],
trim(preg_replace('/\s\s+/', ' ', strip_tags( explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Reparatieomschrijving'][0])[0] ))),
money_format('%+n', $productMeta['_line_total'][0] + $productMeta['_line_tax'][0]),
money_format('%+n', explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Welk wasgoed wil je aanbieden?'][0])[1]),
money_format('%+n', explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Extra behandeling'][0])[1]),
($productMeta['Reparatieomschrijving'][0] ? money_format('%+n', explode(chr(0xE2).chr(0x82).chr(0xAC), $productMeta['Reparatieomschrijving'][0])[1]) : '')
);
}
}
$fp = fopen('php://output', 'w');
foreach ($downloadCSV as $line) {
echo implode(';', $line)."\n";
}
exit();
die();
}
}
?>