/classes/class-wcdn-print.php

  1. <?php 
  2.  
  3. /** 
  4. * Print class 
  5. */ 
  6. if ( ! class_exists( 'WooCommerce_Delivery_Notes_Print' ) ) { 
  7.  
  8. class WooCommerce_Delivery_Notes_Print { 
  9.  
  10. private $template_directory_name; 
  11. private $template_path; 
  12. private $template_default_path; 
  13. private $template_default_uri; 
  14.  
  15. private $order; 
  16.  
  17. public $template_type; 
  18. public $order_id; 
  19.  
  20. /** 
  21. * Constructor 
  22. */ 
  23. public function __construct() {  
  24. global $woocommerce; 
  25. $this->order = new WC_Order(); 
  26. $this->template_directory_name = 'print'; 
  27. $this->template_path = $woocommerce->template_url . $this->template_directory_name . '/'; 
  28. $this->template_default_path = WooCommerce_Delivery_Notes::$plugin_path . 'templates/' . $this->template_directory_name . '/'; 
  29. $this->template_default_uri = WooCommerce_Delivery_Notes::$plugin_url . 'templates/' . $this->template_directory_name . '/'; 
  30.  
  31. /** 
  32. * Load the class 
  33. */ 
  34. public function load() { 
  35. add_action( 'admin_init', array( $this, 'load_hooks' ) ); 
  36.  
  37. /** 
  38. * Load the admin hooks 
  39. */ 
  40. public function load_hooks() {  
  41. add_action('wp_ajax_generate_print_content', array($this, 'generate_print_content_ajax')); 
  42.  
  43. /** 
  44. * Generate the template output 
  45. */ 
  46. public function generate_print_content( $template_type, $order_id ) { 
  47. $this->template_type = $template_type; 
  48. $this->order_id = $order_id; 
  49. $this->order->get_order( $this->order_id ); 
  50. $this->get_template( apply_filters( 'wcdn_template_file_name', 'print-' . $this->template_type . '.php', $template_type, $order_id, $this ) ); 
  51. do_action( 'wcdn_generate_print_content' ); 
  52.  
  53. /** 
  54. * Load and generate the template output with ajax 
  55. */ 
  56. public function generate_print_content_ajax() {  
  57. // Let the backend only access the page 
  58. if( !is_admin() ) { 
  59. wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); 
  60.  
  61. // Check the user privileges 
  62. if( !current_user_can( 'manage_woocommerce_orders' ) && !current_user_can( 'edit_shop_orders' ) ) { 
  63. wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); 
  64.  
  65. // Check the nonce 
  66. if( empty( $_GET['action'] ) || !check_admin_referer( $_GET['action'] ) ) { 
  67. wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); 
  68.  
  69. // Check if all parameters are set 
  70. if( empty( $_GET['template_type'] ) || empty( $_GET['order_id'] ) ) { 
  71. wp_die( __( 'You do not have sufficient permissions to access this page.' ) ); 
  72.  
  73. // Generate the output 
  74. $this->generate_print_content( $_GET['template_type'], $_GET['order_id'] ); 
  75.  
  76. exit; 
  77.  
  78. /** 
  79. * Get the template url for a file. locate by file existience 
  80. * and then return the corresponding url. 
  81. */ 
  82. public function get_template_url( $name ) { 
  83. global $woocommerce; 
  84.  
  85. $uri = $this->template_default_uri . $name; 
  86. $child_theme_path = get_stylesheet_directory() . '/' . $this->template_path; 
  87. $child_theme_uri = get_stylesheet_directory_uri() . '/' . $this->template_path; 
  88. $theme_path = get_template_directory() . '/' . $this->template_path; 
  89. $theme_uri = get_template_directory_uri() . '/' . $this->template_path; 
  90.  
  91. if( file_exists( $child_theme_path . $name ) ) { 
  92. $uri = $child_theme_uri . $name; 
  93. } elseif( file_exists( $theme_path . $name ) ) { 
  94. $uri = $theme_uri . $name; 
  95.  
  96. return $uri; 
  97.  
  98. /** 
  99. * Load the template file content 
  100. */ 
  101. public function get_template( $name ) { 
  102. woocommerce_get_template( $name, null, $this->template_path, $this->template_default_path ); 
  103.  
  104. /** 
  105. * Get the current order 
  106. */ 
  107. public function get_order() { 
  108. return $this->order; 
  109.  
  110. /** 
  111. * Get the current order items 
  112. */ 
  113. public function get_order_items() { 
  114. global $woocommerce; 
  115. global $_product; 
  116.  
  117. $items = $this->order->get_items(); 
  118. $data_list = array(); 
  119.  
  120. if( sizeof( $items ) > 0 ) { 
  121. foreach ( $items as $item ) { 
  122. // Array with data for the printing template 
  123. $data = array(); 
  124.  
  125. // Set the id 
  126. $data['product_id'] = $item['product_id']; 
  127. $data['variation_id'] = $item['variation_id']; 
  128.  
  129. // Set item name 
  130. $data['name'] = $item['name']; 
  131.  
  132. // Set item quantity 
  133. $data['quantity'] = $item['qty']; 
  134.  
  135. // Set the subtotal for the number of products 
  136. $data['line_total'] = $item['line_total']; 
  137. $data['line_tax'] = $item['line_tax']; 
  138.  
  139. // Set the final subtotal for all products 
  140. $data['line_subtotal'] = $item['line_subtotal']; 
  141. $data['line_subtotal_tax'] = $item['line_subtotal_tax']; 
  142. $data['formatted_line_subtotal'] = $this->order->get_formatted_line_subtotal( $item ); 
  143. $data['price'] = $data['formatted_line_subtotal']; 
  144.  
  145. // Set item meta and replace it when it is empty 
  146. $meta = new WC_Order_Item_Meta( $item['item_meta'] );  
  147. $data['meta'] = $meta->display( false, true ); 
  148.  
  149. // Pass complete item array 
  150. $data['item'] = $item; 
  151.  
  152. // Create the product to display more info 
  153. $data['product'] = null; 
  154.  
  155. $product = $this->order->get_product_from_item( $item ); 
  156.  
  157. // Checking fo existance, thanks to MDesigner0  
  158. if(!empty($product)) {  
  159. // Set the single price 
  160. $data['single_price'] = $product->get_price(); 
  161.  
  162. // Set item SKU 
  163. $data['sku'] = $product->get_sku(); 
  164.  
  165. // Set item weight 
  166. $data['weight'] = $product->get_weight(); 
  167.  
  168.  
  169. // Set item dimensions 
  170. $data['dimensions'] = $product->get_dimensions(); 
  171.  
  172. // Pass complete product object 
  173. $data['product'] = $product; 
  174.  
  175.  
  176. $data_list[] = apply_filters( 'wcdn_order_item_data', $data ); 
  177.  
  178. return apply_filters( 'wcdn_order_items_data', $data_list ); 
  179.  
  180. /** 
  181. * Get order custom field 
  182. */ 
  183. public function get_order_field( $field ) { 
  184. if( isset( $this->get_order()->order_custom_fields[$field] ) ) { 
  185. return $this->get_order()->order_custom_fields[$field][0]; 
  186. }  
  187. return; 
  188.  
  189.  
  190. ?> 
.