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. /** Check which order statuses to export. */ 
  73. $order_statuses = ( !empty( $_POST['order_status'] ) && is_array( $_POST['order_status'] ) ) ? $_POST['order_status'] : array_keys( wc_get_order_statuses() ); 
  74.  
  75. $args = array( 'post_type'=>'shop_order', 'posts_per_page'=>-1, 'post_status'=> apply_filters( 'wpg_order_statuses', $order_statuses ) ); 
  76. $args['date_query'] = array( array( 'after'=> $_POST['start_date'], 'before'=> $_POST['end_date'], 'inclusive' => true ) ); 
  77.  
  78. $args = apply_filters( 'wsoe_query_args', $args ); 
  79.  
  80. $orders = new WP_Query( $args ); 
  81.  
  82. if( $orders->have_posts() ) { 
  83.  
  84. /** 
  85. * This will be file pointer 
  86. */ 
  87. $csv_file = self::create_csv_file(); 
  88.  
  89. if( empty($csv_file) ) { 
  90. return new WP_Error( 'not_writable', __( 'Unable to create csv file, upload folder not writable', 'woocommerce-simply-order-export' ) ); 
  91.  
  92. fputcsv( $csv_file, $headings, self::$delimiter ); 
  93.  
  94. /** 
  95. * Loop over each order 
  96. */ 
  97. while( $orders->have_posts() ) { 
  98.  
  99. $csv_values = array(); 
  100.  
  101. $orders->the_post(); 
  102.  
  103. $order_details = new WC_Order( get_the_ID() ); 
  104.  
  105. /** 
  106. * Check if we need to export product name. 
  107. * If yes, then create new row for each product. 
  108. */ 
  109. if( array_key_exists( 'wc_settings_tab_product_name', $fields ) ) { 
  110.  
  111. $items = $order_details->get_items(); 
  112.  
  113. foreach( $items as $item_id=>$item ) { 
  114.  
  115. $csv_values = array(); 
  116. self::add_fields_diff_row( $fields, $csv_values, $order_details, $item_id, $item ); 
  117. fputcsv( $csv_file, $csv_values, self::$delimiter ); 
  118.  
  119. }else{ 
  120. /** 
  121. * Create a single row for order. 
  122. */ 
  123. self::add_fields_diff_row( $fields, $csv_values, $order_details ); 
  124. fputcsv( $csv_file, $csv_values, self::$delimiter ); 
  125.  
  126. wp_reset_postdata(); 
  127.  
  128. }else { 
  129.  
  130. return new WP_Error( 'no_orders', __( 'No orders for specified duration.', 'woocommerce-simply-order-export' ) ); 
  131.  
  132. /** 
  133. *  
  134. */ 
  135. static function add_fields_diff_row( $fields, &$csv_values, $order_details, $item_id = null, $current_item = null ) { 
  136.  
  137. /** 
  138. * Loop over fields and add value for corresponding field. 
  139. */ 
  140. foreach( $fields as $key =>$field ) { 
  141.  
  142. switch ( $key ) { 
  143.  
  144. /** 
  145. * Check if we need order ID. 
  146. */ 
  147. case 'wc_settings_tab_order_id': 
  148. array_push( $csv_values, $order_details->get_order_number() ); 
  149. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  150. break; 
  151.  
  152. /** 
  153. * Check if we need customer name. 
  154. */ 
  155. case 'wc_settings_tab_customer_name': 
  156. array_push( $csv_values, self::customer_name( get_the_ID() ) ); 
  157. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  158. break; 
  159.  
  160. /** 
  161. * Check if we need product name. 
  162. */ 
  163. case 'wc_settings_tab_product_name': 
  164. array_push( $csv_values, $current_item['name'] ); 
  165. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  166. break; 
  167.  
  168. /** 
  169. * Check if we need product quantity. 
  170. * Product quantity will only be exported if user has selected product name to be exported. 
  171. *  
  172. * If product name is not selected, this column will be filled with just dashes. ;) 
  173. */ 
  174. case 'wc_settings_tab_product_quantity': 
  175. if( array_key_exists( 'wc_settings_tab_product_name', $fields ) ) { 
  176. array_push( $csv_values, $current_item['qty'] ); 
  177. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  178. }else{ 
  179. array_push( $csv_values, '-' ); // pad the quantity column with dash if there is no product name 
  180. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  181. break; 
  182.  
  183. /** 
  184. * Check if we need product variations 
  185. */ 
  186. case 'wc_settings_tab_product_variation': 
  187. if( array_key_exists( 'wc_settings_tab_product_name', $fields ) ) { 
  188. array_push( $csv_values, self::get_product_variation( $item_id, $order_details ) ); 
  189. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  190. }else{ 
  191. array_push( $csv_values, '-' ); 
  192. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  193. break; 
  194.  
  195. /** 
  196. * Check if we need order amount. 
  197. */ 
  198. case 'wc_settings_tab_amount': 
  199. //$amount = wc_price( $order_details->get_total(), array( 'currency'=> $order_details->get_order_currency() ) ); 
  200. array_push( $csv_values, wsoe_formatted_price( $order_details->get_total(), $order_details ) ); 
  201. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  202. break; 
  203.  
  204. /** 
  205. * Check if we need customer email. 
  206. */ 
  207. case 'wc_settings_tab_customer_email': 
  208. array_push( $csv_values, self::customer_meta( get_the_ID(), '_billing_email' ) ); 
  209. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  210. break; 
  211.  
  212. /** 
  213. * Check if we need customer phone. 
  214. */ 
  215. case 'wc_settings_tab_customer_phone': 
  216. array_push( $csv_values, self::customer_meta( get_the_ID(), '_billing_phone' ) ); 
  217. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  218. break; 
  219.  
  220. /** 
  221. * Check if we need order status. 
  222. */ 
  223. case 'wc_settings_tab_order_status': 
  224. array_push( $csv_values, ucwords($order_details->get_status()) ); 
  225. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  226. break; 
  227.  
  228. default : 
  229. /** 
  230. * Add values to CSV. 
  231. *  
  232. * @param array $csv_values Array of csv values, callback function should accept this argument by reference. 
  233. *  
  234. * @param Object $order_details WC_Order object 
  235. *  
  236. * @param String $key Current key in loop. 
  237. *  
  238. */ 
  239. do_action_ref_array( 'wpg_add_values_to_csv', array( &$csv_values, $order_details, $key, $fields, $item_id, $current_item ) ); 
  240. do_action_ref_array( 'wsoe_after_value_'.$key, array( &$csv_values, $order_details, $item_id, $current_item ) ); 
  241. break; 
  242.  
  243.  
  244. /** 
  245. * Returns customer related meta. 
  246. * Basically it is just get_post_meta() function wrapper. 
  247. */ 
  248. static function customer_meta( $order_id , $meta = '' ) { 
  249.  
  250. if( empty( $order_id ) || empty( $meta ) ) 
  251. return ''; 
  252.  
  253. return get_post_meta( $order_id, $meta, true ); 
  254.  
  255. static function get_product_variation ( $product_id, $order_details ) { 
  256.  
  257. $metadata = $order_details->has_meta( $product_id ); 
  258. $_product = new WC_Product( $product_id ); 
  259.  
  260. $exclude_meta = apply_filters( 'woocommerce_hidden_order_itemmeta', array( 
  261. '_qty',  
  262. '_tax_class',  
  263. '_product_id',  
  264. '_variation_id',  
  265. '_line_subtotal',  
  266. '_line_subtotal_tax',  
  267. '_line_total',  
  268. '_line_tax',  
  269. ) ); 
  270.  
  271. $variation_details = array(); 
  272.  
  273. foreach( $metadata as $k => $meta ) { 
  274.  
  275. if( in_array( $meta['meta_key'], $exclude_meta ) ) { 
  276. continue; 
  277.  
  278. // Skip serialised meta 
  279. if ( is_serialized( $meta['meta_value'] ) ) { 
  280. continue; 
  281.  
  282. // Get attribute data 
  283. if ( taxonomy_exists( wc_sanitize_taxonomy_name( $meta['meta_key'] ) ) ) { 
  284.  
  285. $term = get_term_by( 'slug', $meta['meta_value'], wc_sanitize_taxonomy_name( $meta['meta_key'] ) ); 
  286. $meta['meta_key'] = wc_attribute_label( wc_sanitize_taxonomy_name( $meta['meta_key'] ) ); 
  287. $meta['meta_value'] = isset( $term->name ) ? $term->name : $meta['meta_value']; 
  288.  
  289. }else { 
  290. $meta['meta_key'] = apply_filters( 'woocommerce_attribute_label', wc_attribute_label( $meta['meta_key'], $_product ), $meta['meta_key'] ); 
  291.  
  292. array_push( $variation_details, wp_kses_post( urldecode( $meta['meta_key'] ) ) .': '.wp_kses_post( urldecode( $meta['meta_value'] ) ) ); 
  293.  
  294. return $variation_details = implode( ' | ', $variation_details ); 
  295.  
  296. /** 
  297. * Returns customer name for particular order 
  298. * @param type $order_id 
  299. * @return string 
  300. */ 
  301. static function customer_name( $order_id ) { 
  302.  
  303. if( empty( $order_id ) ) { 
  304. return ''; 
  305.  
  306. $firstname = get_post_meta( $order_id, '_billing_first_name', true ); 
  307. $lastname = get_post_meta( $order_id, '_billing_last_name', true ); 
  308.  
  309. return trim( $firstname.' '. $lastname );  
  310.  
  311. /** 
  312. * Makes first row for csv 
  313. */ 
  314. static function csv_heading( $fields ) { 
  315.  
  316. if( !is_array( $fields ) ) { 
  317. return false; 
  318.  
  319. global $wpg_order_columns; 
  320. $headings = array(); 
  321.  
  322. foreach( $fields as $key=>$val ) { 
  323.  
  324. if( $val === true && array_key_exists( $key, $wpg_order_columns ) ) { 
  325. array_push( $headings, $wpg_order_columns[$key] ); 
  326. // By using this we can add heading for keys which are not sanitized. 
  327. do_action_ref_array( 'wsoe_after_heading_'.$key, array( &$headings ) ); 
  328.  
  329. return $headings; 
  330.  
  331.  
  332. /** 
  333. * Creates csv file in upload directory. 
  334. */ 
  335. static function create_csv_file() { 
  336.  
  337. $csv_filename = empty($_POST['woo_soe_csv_name']) ? 'order_export.csv' : sanitize_file_name($_POST['woo_soe_csv_name']) .'.csv'; 
  338. $new_filename = wp_unique_filename( trailingslashit( wsoe_upload_dir() ), $csv_filename ); 
  339.  
  340. $csv_file = fopen( trailingslashit( wsoe_upload_dir() ) . $new_filename, 'w+' ); 
  341.  
  342. do_action( 'wsoe_file_created', $_POST['start_date'], $_POST['end_date'], $new_filename, $csv_file ); 
  343.  
  344. /** 
  345. * Save file name in global for later use. 
  346. */ 
  347. $GLOBALS['wsoe_filename'] = str_replace( '.csv', '', $new_filename ); 
  348. return $csv_file;