WooCommerce_Delivery_Notes_Print

The WooCommerce Print Invoices & Delivery Notes WooCommerce Delivery Notes Print class.

Defined (1)

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

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