Printpage

<?php

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

// create custom plugin settings menu
add_action('admin_menu', 'add_print_page_woocommerce');
function add_print_page_woocommerce() {
	add_submenu_page('woocommerce', 'Print labels', 'Print labels', 'administrator', 'printlabel', 'print_label_page');
}

//create custom print page
function print_label_page() {
  ?>
  <div class="wrap">
    <h1>Print Label</h1>
    <?php
    if(isset($_GET['order_id'])) {

      $order = new WC_Order( $_GET['order_id'] );
      $order_meta = get_post_meta($_GET['order_id']);
      $items = $order->get_items();
      foreach ($items as $key => $product ) {
        $productMeta = $product['item_meta'];
        //echo '<pre>'; print_r($productMeta); echo '</pre>';
        for($i = 0; $i < $product['qty']; $i++) {
          echo '------------------------------<br>';
          echo 'Ordernr: '.$_GET['order_id'].'<br>';
          echo 'Orderdatum: '.$order->order_date.'<br>';
          echo ($order->customer_message ? 'Klantbericht: '.$order->customer_message.'<br>' : '');
          echo 'Klantnaam: '.$order_meta['_billing_first_name'][0].' '.$order_meta['_billing_last_name'][0].'<br>';
          echo 'Klantcontact: '.$order_meta['_billing_email'][0].', '.$order_meta['_billing_phone'][0].'<br>';
          echo 'Klantadres: '.$order_meta['_billing_country'][0].', '.$order_meta['_billing_city'][0].'. '.$order_meta['_billing_address_1'][0].$order_meta['_billing_address_2'][0].'<br>';
          echo 'Productnr: '.$product['product_id'].'<br>';
          echo 'Type: '.$productMeta['Type'][0].'<br>';
          echo 'Eigenschappen: '.$productMeta['Eigenschappen'][0].'<br>';
          echo 'Reparatieomschrijving: '.$productMeta['Reparatieomschrijving'][0].'<br>';
          echo 'Naam eigenaar: '.$productMeta['Naam eigenaar'][0].'<br>';
          echo 'Nummer: '.($i+1).'<br>';
          echo 'Label: '.unserialize($product['labels'])[$i].'<br>';
          echo 'Datum: '.date('d-m-Y').'<br>';
          echo '<img src="'.get_stylesheet_directory_uri().'/barcode.php?text='.unserialize($product['labels'])[$i].'&print=true"><br>';
        }
      }
      echo '<a href="javascript:window.print();">Print pagina</a>';
    } else {
      echo 'Er ging iets mis.. <br>';
      echo '<a class="btn btn-primary" href="'.admin_url( 'edit.php?post_type=shop_order' ).'">Terug naar overzicht</a>';
    }
    ?>
  </div>
  <?php
}
?>
Last Updated:
Contributors: Niek Vlam, Suite Seven