WC_Flespakket_Writepanel

The WooCommerce Flespakket WC Flespakket Writepanel class.

Defined (1)

The class is defined in the following location(s).

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