order_export_process

The WooCommerce Simply Order Export order export process class.

Defined (1)

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

/classes/class-order-export-process.php  
  1. class order_export_process { 
  2.  
  3. /** 
  4. * Delimiter for CSV file 
  5. */ 
  6. static $delimiter; 
  7.  
  8. /** 
  9. * Tells which fields to export. 
  10. *  
  11. * Also reset orders of fields according to which they were added 
  12. * in plugin. 
  13. *  
  14. * Refer this support link: https://wordpress.org/support/topic/change-order-5?replies=7#post-6818741 
  15. */ 
  16. static function export_options() { 
  17.  
  18. global $wpg_order_columns; 
  19.  
  20. $settings = wpg_order_export::get_settings_fields(); 
  21. $settings = apply_filters( 'wpg_export_options_settings', $settings ); 
  22.  
  23. $fields = array(); 
  24.  
  25. $setting_order = array(); 
  26.  
  27. if( is_array( $settings ) ) { 
  28.  
  29. foreach( $settings as $setting ) { 
  30.  
  31. if( !is_array( $setting ) || empty($setting['id']) ) { 
  32. continue; 
  33.  
  34. array_push( $setting_order, $setting['id'] ); 
  35.  
  36. $new_order = array(); 
  37.  
  38. foreach( $setting_order as $_setting ) { 
  39.  
  40. if( empty( $wpg_order_columns[$_setting] ) ) { 
  41. continue; 
  42.  
  43. $new_order[$_setting] = $wpg_order_columns[$_setting]; 
  44.  
  45. $wpg_order_columns = $new_order; 
  46.  
  47. foreach( $wpg_order_columns as $key=>$val ) { 
  48.  
  49. $retireve = get_option( $key, 'no' ); 
  50. $fields[$key] = ( strtolower($retireve) === 'yes' ) ? true : false; 
  51.  
  52. return $fields; 
  53.  
  54. /** 
  55. * Returns order details 
  56. */ 
  57. static function get_orders() { 
  58.  
  59. $fields = self::export_options(); 
  60. $fields = array_filter( $fields, 'wsoe_array_filter' ); 
  61. $headings = self::csv_heading($fields); 
  62.  
  63. $delimiter = ( empty( $_POST['wpg_delimiter'] ) || ( gettype( $_POST['wpg_delimiter'] ) !== 'string' ) ) ? ', ' : $_POST['wpg_delimiter'][0]; 
  64.  
  65. /** 
  66. * Filter : wpg_delimiter 
  67. * Filters the delimiter for exported csv file. Override user defined 
  68. * delimiter by using this filter. 
  69. */ 
  70. self::$delimiter = apply_filters( 'wpg_delimiter', $delimiter ); 
  71.  
  72. $iterator = 0; 
  73. $chunk_size = apply_filters( 'wsoe_chunk_size', 20 ); 
  74.  
  75. @set_time_limit(0); 
  76. do{ 
  77.  
  78. /** Check which order statuses to export. */ 
  79. $order_statuses = ( !empty( $_POST['order_status'] ) && is_array( $_POST['order_status'] ) ) ? $_POST['order_status'] : array_keys( wc_get_order_statuses() ); 
  80.  
  81. $args = array( 
  82. 'post_type'=>'shop_order',  
  83. 'posts_per_page'=> $chunk_size,  
  84. 'post_status'=> apply_filters( 'wpg_order_statuses', $order_statuses ),  
  85. 'offset' => ($chunk_size*$iterator) 
  86. ); 
  87.  
  88. $args['date_query'] = array( 
  89. array( 
  90. 'after'=> $_POST['start_date'],  
  91. 'before'=> $_POST['end_date'],  
  92. 'inclusive' => true ) 
  93. ); 
  94.  
  95. $args = apply_filters( 'wsoe_query_args', $args ); 
  96.  
  97. $orders = new WP_Query( $args ); 
  98.  
  99. if( $orders->have_posts() ) { 
  100.  
  101. /** 
  102. * Do these operations for only first iteration. 
  103. */ 
  104. if( !$iterator ) { 
  105.  
  106. /** 
  107. * This will be file pointer 
  108. */ 
  109. $csv_file = self::create_csv_file(); 
  110.  
  111. if( empty($csv_file) ) { 
  112. return new WP_Error( 'not_writable', __( 'Unable to create csv file, upload folder not writable', 'woocommerce-simply-order-export' ) ); 
  113.  
  114. fputcsv( $csv_file, $headings, self::$delimiter ); 
  115.  
  116.  
  117. /** 
  118. * Loop over each order 
  119. */ 
  120. while( $orders->have_posts() ) { 
  121.  
  122. $csv_values = array(); 
  123.  
  124. $orders->the_post(); 
  125.  
  126. $order_details = new WC_Order( get_the_ID() ); 
  127.  
  128. $items = $order_details->get_items(); 
  129.  
  130. foreach( $items as $item_id=>$item ) { 
  131.  
  132. $csv_values = array(); 
  133. self::add_fields_diff_row( $fields, $csv_values, $order_details, $item_id, $item ); 
  134. fputcsv( $csv_file, $csv_values, self::$delimiter ); 
  135.  
  136. wp_reset_postdata(); 
  137.  
  138. }elseif( empty($iterator) ) { // If there are no orders at first place, send error. 
  139.  
  140. return new WP_Error( 'no_orders', __( 'No orders for specified duration.', 'woocommerce-simply-order-export' ) ); 
  141.  
  142. ++$iterator; 
  143.  
  144. }while( ( $iterator < $orders->max_num_pages ) ); 
  145.  
  146. /** 
  147. *  
  148. */ 
  149. static function add_fields_diff_row( $fields, &$csv_values, $order_details, $item_id = null, $current_item = null ) { 
  150.  
  151. /** 
  152. * Loop over fields and add value for corresponding field. 
  153. */ 
  154. foreach( $fields as $key =>$field ) { 
  155.  
  156. switch ( $key ) { 
  157.  
  158. /** 
  159. * Check if we need order ID. 
  160. */ 
  161. case 'wc_settings_tab_order_id': 
  162. array_push( $csv_values, $order_details->get_order_number() ); 
  163. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  164. break; 
  165.  
  166. /** 
  167. * Check if we need customer name. 
  168. */ 
  169. case 'wc_settings_tab_customer_name': 
  170. array_push( $csv_values, self::customer_name( get_the_ID() ) ); 
  171. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  172. break; 
  173.  
  174. /** 
  175. * Check if we need product name. 
  176. */ 
  177. case 'wc_settings_tab_product_name': 
  178. array_push( $csv_values, $current_item['name'] ); 
  179. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  180. break; 
  181.  
  182. /** 
  183. * Check if we need product quantity. 
  184. * Product quantity will only be exported if user has selected product name to be exported. 
  185. *  
  186. * If product name is not selected, this column will be filled with just dashes. ;) 
  187. */ 
  188. case 'wc_settings_tab_product_quantity': 
  189. array_push( $csv_values, $current_item['qty'] ); 
  190. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  191. break; 
  192.  
  193. /** 
  194. * Check if we need product variations 
  195. */ 
  196. case 'wc_settings_tab_product_variation': 
  197. array_push( $csv_values, self::get_product_variation( $item_id, $order_details ) ); 
  198. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  199. break; 
  200.  
  201. /** 
  202. * Check if we need order amount. 
  203. */ 
  204. case 'wc_settings_tab_amount': 
  205. array_push( $csv_values, wsoe_formatted_price( $order_details->get_total(), $order_details ) ); 
  206. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  207. break; 
  208.  
  209. /** 
  210. * Check if we need customer email. 
  211. */ 
  212. case 'wc_settings_tab_customer_email': 
  213. array_push( $csv_values, self::customer_meta( get_the_ID(), '_billing_email' ) ); 
  214. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  215. break; 
  216.  
  217. /** 
  218. * Check if we need customer phone. 
  219. */ 
  220. case 'wc_settings_tab_customer_phone': 
  221. array_push( $csv_values, self::customer_meta( get_the_ID(), '_billing_phone' ) ); 
  222. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  223. break; 
  224.  
  225. /** 
  226. * Check if we need order status. 
  227. */ 
  228. case 'wc_settings_tab_order_status': 
  229. array_push( $csv_values, ucwords($order_details->get_status()) ); 
  230. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  231. break; 
  232.  
  233. default : 
  234. /** 
  235. * Add values to CSV. 
  236. *  
  237. * @param array $csv_values Array of csv values, callback function should accept this argument by reference. 
  238. *  
  239. * @param Object $order_details WC_Order object 
  240. *  
  241. * @param String $key Current key in loop. 
  242. *  
  243. */ 
  244. do_action_ref_array( 'wpg_add_values_to_csv', array( &$csv_values, $order_details, $key, $fields, $item_id, $current_item ) ); 
  245. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  246. break; 
  247.  
  248.  
  249. /** 
  250. * Returns customer related meta. 
  251. * Basically it is just get_post_meta() function wrapper. 
  252. */ 
  253. static function customer_meta( $order_id , $meta = '' ) { 
  254.  
  255. if( empty( $order_id ) || empty( $meta ) ) 
  256. return ''; 
  257.  
  258. return get_post_meta( $order_id, $meta, true ); 
  259.  
  260. static function get_product_variation ( $product_id, $order_details ) { 
  261.  
  262. $metadata = $order_details->has_meta( $product_id ); 
  263. $_product = new WC_Product( $product_id ); 
  264.  
  265. $exclude_meta = apply_filters( 'woocommerce_hidden_order_itemmeta', array( 
  266. '_qty',  
  267. '_tax_class',  
  268. '_product_id',  
  269. '_variation_id',  
  270. '_line_subtotal',  
  271. '_line_subtotal_tax',  
  272. '_line_total',  
  273. '_line_tax',  
  274. ) ); 
  275.  
  276. $variation_details = array(); 
  277.  
  278. foreach( $metadata as $k => $meta ) { 
  279.  
  280. if( in_array( $meta['meta_key'], $exclude_meta ) ) { 
  281. continue; 
  282.  
  283. // Skip serialised meta 
  284. if ( is_serialized( $meta['meta_value'] ) ) { 
  285. continue; 
  286.  
  287. // Get attribute data 
  288. if ( taxonomy_exists( wc_sanitize_taxonomy_name( $meta['meta_key'] ) ) ) { 
  289.  
  290. $term = get_term_by( 'slug', $meta['meta_value'], wc_sanitize_taxonomy_name( $meta['meta_key'] ) ); 
  291. $meta['meta_key'] = wc_attribute_label( wc_sanitize_taxonomy_name( $meta['meta_key'] ) ); 
  292. $meta['meta_value'] = isset( $term->name ) ? $term->name : $meta['meta_value']; 
  293.  
  294. }else { 
  295. $meta['meta_key'] = apply_filters( 'woocommerce_attribute_label', wc_attribute_label( $meta['meta_key'], $_product ), $meta['meta_key'] ); 
  296.  
  297. array_push( $variation_details, wp_kses_post( urldecode( $meta['meta_key'] ) ) .': '.wp_kses_post( urldecode( $meta['meta_value'] ) ) ); 
  298.  
  299. return $variation_details = implode( ' | ', $variation_details ); 
  300.  
  301. /** 
  302. * Returns customer name for particular order 
  303. * @param type $order_id 
  304. * @return string 
  305. */ 
  306. static function customer_name( $order_id ) { 
  307.  
  308. if( empty( $order_id ) ) { 
  309. return ''; 
  310.  
  311. $firstname = get_post_meta( $order_id, '_billing_first_name', true ); 
  312. $lastname = get_post_meta( $order_id, '_billing_last_name', true ); 
  313.  
  314. return trim( $firstname.' '. $lastname );  
  315.  
  316. /** 
  317. * Makes first row for csv 
  318. */ 
  319. static function csv_heading( $fields ) { 
  320.  
  321. if( !is_array( $fields ) ) { 
  322. return false; 
  323.  
  324. global $wpg_order_columns; 
  325. $headings = array(); 
  326.  
  327. foreach( $fields as $key=>$val ) { 
  328.  
  329. if( $val === true && array_key_exists( $key, $wpg_order_columns ) ) { 
  330. array_push( $headings, $wpg_order_columns[$key] ); 
  331. // By using this we can add heading for keys which are not sanitized. 
  332. do_action_ref_array( 'wsoe_after_heading_'.$key, array( &$headings ) ); 
  333.  
  334. return $headings; 
  335.  
  336.  
  337. /** 
  338. * Creates csv file in upload directory. 
  339. */ 
  340. static function create_csv_file() { 
  341.  
  342. $csv_filename = empty($_POST['woo_soe_csv_name']) ? 'order_export.csv' : sanitize_file_name($_POST['woo_soe_csv_name']) .'.csv'; 
  343. $new_filename = wp_unique_filename( trailingslashit( wsoe_upload_dir() ), $csv_filename ); 
  344.  
  345. $csv_file = fopen( trailingslashit( wsoe_upload_dir() ) . $new_filename, 'w+' ); 
  346.  
  347. do_action( 'wsoe_file_created', $_POST['start_date'], $_POST['end_date'], $new_filename, $csv_file ); 
  348.  
  349. /** 
  350. * Save file name in global for later use. 
  351. */ 
  352. $GLOBALS['wsoe_filename'] = str_replace( '.csv', '', $new_filename ); 
  353. return $csv_file;