/includes/wcflespakket-writepanel.php

  1. <?php 
  2. class WC_Flespakket_Writepanel { 
  3.  
  4. public function __construct() { 
  5. $this->settings = get_option( 'wcflespakket_settings' ); 
  6.  
  7. // Add meta box with Flespakket links/buttons 
  8. add_action( 'add_meta_boxes_shop_order', array( $this, 'add_box' ) ); 
  9.  
  10. // Add export action to drop down menu 
  11. add_action( 'admin_footer', array( &$this, 'export_actions' ) );  
  12.  
  13. // Add buttons in order listing 
  14. add_action( 'woocommerce_admin_order_actions_end', array( $this, 'add_listing_actions' ), 20 ); 
  15.  
  16. // Customer Emails 
  17. if (isset($this->settings['email_tracktrace'])) { 
  18. add_action( 'woocommerce_email_before_order_table', array( $this, 'track_trace_email' ), 10, 2 ); 
  19.  
  20. // Track & trace in my account 
  21. if (isset($this->settings['myaccount_tracktrace'])) { 
  22. add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'track_trace_myaccount' ), 10, 2 ); 
  23.  
  24. // Pakjegemak 
  25. if (isset($this->settings['pakjegemak'])) { 
  26. add_action( apply_filters( 'wcflespakket_pakjegemak_locatie', 'woocommerce_checkout_before_customer_details' ), array( $this, 'pakjegemak' ), 10, 1 ); 
  27.  
  28.  
  29. /** 
  30. * Add the meta box on the single order page 
  31. */ 
  32. public function add_box() { 
  33. add_meta_box( 
  34. 'flespakket', //$id 
  35. __( 'Flespakket', 'wcflespakket' ), //$title 
  36. array( $this, 'create_box_content' ), //$callback 
  37. 'shop_order', //$post_type 
  38. 'side', //$context 
  39. 'default' //$priority 
  40. ); 
  41.  
  42. /** 
  43. * Callback: Create the meta box content on the single order page 
  44. */ 
  45. public function create_box_content() { 
  46. global $post_id; 
  47. $pdf_link = wp_nonce_url( admin_url( 'edit.php?&action=wcflespakket-label&order_ids=' . $post_id ), 'wcflespakket-label' ); 
  48. $export_link = wp_nonce_url( admin_url( 'edit.php?&action=wcflespakket&order_ids=' . $post_id ), 'wcflespakket' ); 
  49.  
  50. $target = ( isset($this->settings['download_display']) && $this->settings['download_display'] == 'display') ? 'target="_blank"' : ''; 
  51.  
  52.  
  53. if (get_post_meta($post_id, '_flespakket_consignment_id', true)) { 
  54. $consignment_id = get_post_meta($post_id, '_flespakket_consignment_id', true); 
  55.  
  56. $tracktrace = get_post_meta($post_id, '_flespakket_tracktrace', true); 
  57. $tracktrace_url = $this->get_tracktrace_url($post_id); 
  58.  
  59.  
  60. // fetch TNT status 
  61. $tnt_status_url = 'http://www.flespakket.nl/status/tnt/' . $consignment_id; 
  62. $tnt_status = explode('|', @file_get_contents($tnt_status_url)); 
  63. $tnt_status = (count($tnt_status) == 3) ? $tnt_status[2] : ''; 
  64.  
  65. ?> 
  66. <ul> 
  67. <li> 
  68. <a href="<?php echo $pdf_link; ?>" class="button" alt="Download label (PDF)" <?php echo $target; ?>>Download label (PDF)</a> 
  69. </li> 
  70. <li>Status: <?php echo $tnt_status ?></li> 
  71. <li>Track&Trace code: <a href="<?php echo $tracktrace_url; ?>"><?php echo $tracktrace; ?></a></li> 
  72. <li> 
  73. <a href="<?php echo $export_link; ?>" class="button flespakket one-flespakket" alt="Exporteer naar Flespakket">Exporteer opnieuw</a> 
  74. </li> 
  75. </ul> 
  76. <?php 
  77. } else { 
  78. ?> 
  79. <ul> 
  80. <li> 
  81. <a href="<?php echo $export_link; ?>" class="button flespakket one-flespakket" alt="Exporteer naar Flespakket">Exporteer naar Flespakket</a> 
  82. </li> 
  83. </ul> 
  84. <?php  
  85.  
  86. /** 
  87. * Add export option to bulk action drop down menu 
  88. * 
  89. * Using Javascript until WordPress core fixes: http://core.trac.wordpress.org/ticket/16031 
  90. * 
  91. * @access public 
  92. * @return void 
  93. */ 
  94. public function export_actions() { 
  95. global $post_type; 
  96.  
  97. if ( 'shop_order' == $post_type ) { 
  98. ?> 
  99. <script type="text/javascript"> 
  100. jQuery(document).ready(function() { 
  101. jQuery('<option>').val('wcflespakket').text('<?php _e( 'Exporteer naar Flespakket', 'wcflespakket' )?>').appendTo("select[name='action']"); 
  102. jQuery('<option>').val('wcflespakket').text('<?php _e( 'Exporteer naar Flespakket', 'wcflespakket' )?>').appendTo("select[name='action2']"); 
  103.  
  104. jQuery('<option>').val('wcflespakket-label').text('<?php _e( 'Print Flespakket labels', 'wcflespakket' )?>').appendTo("select[name='action']"); 
  105. jQuery('<option>').val('wcflespakket-label').text('<?php _e( 'Print Flespakket labels', 'wcflespakket' )?>').appendTo("select[name='action2']"); 
  106. }); 
  107. </script> 
  108. <?php 
  109. }  
  110.  
  111. /** 
  112. * Add print actions to the orders listing 
  113. */ 
  114. public function add_listing_actions( $order ) { 
  115. $consignment_id = get_post_meta($order->id, '_flespakket_consignment_id', true); 
  116.  
  117. $pdf_link = wp_nonce_url( admin_url( 'edit.php?&action=wcflespakket-label&order_ids=' . $order->id ), 'wcflespakket-label' ); 
  118. $export_link = wp_nonce_url( admin_url( 'edit.php?&action=wcflespakket&order_ids=' . $order->id ), 'wcflespakket' ); 
  119.  
  120. $target = ( isset($this->settings['download_display']) && $this->settings['download_display'] == 'display') ? 'target="_blank"' : ''; 
  121. if (!empty($consignment_id)) { 
  122. ?> 
  123. <a href="<?php echo $pdf_link; ?>" class="button tips flespakket" alt="Print Flespakket label" data-tip="Print Flespakket label" <?php echo $target; ?>> 
  124. <img src="<?php echo dirname(plugin_dir_url(__FILE__)) . '/img/flespakket-pdf.png'; ?>" alt="Print Flespakket label"> 
  125. </a> 
  126. <a href="<?php echo $export_link; ?>" class="button tips flespakket one-flespakket" alt="Exporteer naar Flespakket" data-tip="Exporteer naar Flespakket"> 
  127. <img src="<?php echo dirname(plugin_dir_url(__FILE__)) . '/img/flespakket-up.png'; ?>" alt="Exporteer naar Flespakket"> 
  128. </a> 
  129. <?php 
  130. } else { 
  131. ?> 
  132. <a href="<?php echo $export_link; ?>" class="button tips flespakket one-flespakket" alt="Exporteer naar Flespakket" data-tip="Exporteer naar Flespakket"> 
  133. <img src="<?php echo dirname(plugin_dir_url(__FILE__)) . '/img/flespakket-up.png'; ?>" alt="Exporteer naar Flespakket"> 
  134. </a> 
  135. <?php 
  136.  
  137.  
  138. /** 
  139. * Add track&trace to user email 
  140. **/ 
  141.  
  142. public function track_trace_email( $order, $sent_to_admin ) { 
  143.  
  144. if ( $sent_to_admin ) return; 
  145.  
  146. if ( $order->status != 'completed') return; 
  147.  
  148. $tracktrace = get_post_meta($order->id, '_flespakket_tracktrace', true); 
  149. if ( !empty($tracktrace) ) { 
  150. $tracktrace_url = $this->get_tracktrace_url($order->id); 
  151.  
  152. $tracktrace_link = '<a href="'.$tracktrace_url.'">'.$tracktrace.'</a>'; 
  153. $email_text = apply_filters( 'wcflespakket_email_text', 'U kunt uw bestelling volgen met het volgende PostNL track&trace nummer:' ); 
  154. ?> 
  155. <p><?php echo $email_text.' '.$tracktrace_link; ?></p> 
  156.  
  157. <?php 
  158.  
  159. public function track_trace_myaccount( $actions, $order ) { 
  160. $tracktrace = get_post_meta($order->id, '_flespakket_tracktrace', true); 
  161. if ( !empty($tracktrace) ) { 
  162. $tracktrace_url = $this->get_tracktrace_url($order->id); 
  163.  
  164. $actions['flespakket_tracktrace'] = array( 
  165. 'url' => $tracktrace_url,  
  166. 'name' => apply_filters( 'wcflespakket_myaccount_tracktrace_button', __( 'Track&Trace', 'wpo_wcpdf' ) ) 
  167. );  
  168.  
  169. return $actions; 
  170.  
  171. public function get_tracktrace_url($order_id) { 
  172. if (empty($order_id)) 
  173. return; 
  174.  
  175. $tracktrace = get_post_meta($order_id, '_flespakket_tracktrace', true); 
  176. $postcode = preg_replace('/\s+/', '', get_post_meta($order_id, '_shipping_postcode', true)); 
  177. $tracktrace_url = sprintf('https://mijnpakket.postnl.nl/Claim?Barcode=%s&Postalcode=%s', $tracktrace, $postcode); 
  178.  
  179. //Check if foreign 
  180. $country = get_post_meta($order_id, '_shipping_country', true); 
  181. if ($country != 'NL') 
  182. $tracktrace_url = add_query_arg( 'Foreign', 'True', $tracktrace_url ); 
  183.  
  184. return $tracktrace_url; 
  185.  
  186. /** 
  187. * Add pakjegemak button to checkout 
  188. */ 
  189.  
  190. public function pakjegemak() { 
  191. $username = $this->settings['api_username']; 
  192. $api_key = $this->settings['api_key']; 
  193.  
  194. $webshop = plugin_dir_url( __FILE__ ) . '/wcflespakket-pakjegemak-passdata.html'; 
  195. $hash = hash_hmac('sha1', $username . 'Flespakket' . $webshop, $api_key); 
  196.  
  197. // check for secure context 
  198. $context = is_ssl() ? 'https' : 'http'; 
  199.  
  200. $popup_url = sprintf('%s://www.flespakket.nl/pakjegemak-locatie?hash=%s&webshop=%s&user=%s', $context, $hash, $webshop, $username); 
  201.  
  202.  
  203. if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '<=' ) ) { 
  204. // old versions use 'shiptobilling' 
  205. $stda = 'shiptobilling'; 
  206. $stda_checked = 'false'; // uncheck for alternate shipping address  
  207. } else { 
  208. // WC2.1 
  209. $stda = 'ship-to-different-address'; 
  210. $stda_checked = 'true'; // check for alternate shipping address  
  211.  
  212. // Create page text/HTML 
  213. $omschrijving = $this->settings['pakjegemak_description']; 
  214. $knop = $this->settings['pakjegemak_button']; 
  215.  
  216. ob_start(); 
  217. ?> 
  218. <div class="flespakket-pakjegemak" style="overflow:auto;"> 
  219. <span class="flespakket-pakjegemak-omschrijving"><?php echo $omschrijving; ?></span> 
  220. <a class="flespakket-pakjegemak button" onclick="return pakjegemak();" style="cursor:pointer; float:right; margin:1em 0"><?php echo $knop; ?></a> 
  221. </div> 
  222. <?php 
  223. // gebruik het filter om je eigen HTML/tekst weer te geven 
  224. echo apply_filters( 'wcflespakket_pakjegemak_html', ob_get_clean() ); 
  225. ?> 
  226. <script type="text/javascript"> 
  227. var pg_popup; 
  228. function pakjegemak() 
  229. jQuery( '#<?php echo $stda;?> input' ).prop('checked', <?php echo $stda_checked;?>); 
  230. jQuery( '#<?php echo $stda;?> input' ).change(); 
  231. jQuery( '#shipping_country' ).val('NL'); 
  232. jQuery( '#shipping_country' ).trigger("chosen:updated") 
  233. jQuery( '#shipping_country' ).change(); 
  234.  
  235. if(!pg_popup || pg_popup.closed) 
  236. pg_popup = window.open( 
  237. '<?php echo $popup_url; ?>',  
  238. 'flespakket-pakjegemak',  
  239. 'width=980, height=680, dependent, resizable, scrollbars' 
  240. ); 
  241. if(window.focus) { pg_popup.focus(); } 
  242. else 
  243. pg_popup.focus(); 
  244. return false; 
  245. </script> 
  246. <?php 
  247. }  
.