id = 'pdf_invoicing_display'; $this->parent_id = 'pdf_invoicing'; $this->short_desc = __( 'Display & Misc.', 'all-in-one-wc' ); $this->desc = ''; parent::__construct( 'submodule' ); if ( $this->is_enabled() ) { // Columns on Admin's Orders page. add_filter( 'manage_edit-shop_order_columns', array( $this, 'add_order_column' ), PHP_INT_MAX - 3 ); add_action( 'manage_shop_order_posts_custom_column', array( $this, 'render_order_columns' ), 2 ); // Action Links on Customer's My Account page. add_filter( 'woocommerce_my_account_my_orders_actions', array( $this, 'add_pdf_invoices_action_links' ), PHP_INT_MAX, 2 ); // Action Links on Customer's Thank You page. add_action( 'woocommerce_thankyou', array( $this, 'add_pdf_invoices_links_to_thankyou_page' ), 10, 1 ); // Action Buttons to Admin's Orders list. add_filter( 'woocommerce_admin_order_actions', array( $this, 'add_pdf_invoices_admin_actions' ), PHP_INT_MAX, 2 ); add_filter( 'admin_head', array( $this, 'add_pdf_invoices_admin_actions_buttons_css' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Make Sortable Columns. add_filter( 'manage_edit-shop_order_sortable_columns', array( $this, 'shop_order_sortable_columns' ) ); add_action( 'pre_get_posts', array( $this, 'shop_order_pre_get_posts_order_by_column' ) ); // Meta box on admin order page. add_action( 'add_meta_boxes', array( $this, 'add_invoices_meta_box' ) ); } } /** * WP Table column: post type `Order`. * * @param object $query WP_Query. */ function shop_order_pre_get_posts_order_by_column( $query ) { if ( $query->is_main_query() && ( $orderby = $query->get( 'orderby' ) ) && isset( $query->query['post_type'] ) && 'shop_order' === $query->query['post_type'] && isset( $query->is_admin ) && 1 == $query->is_admin ) { if ( 'aiow_invoicing_' === substr( $orderby, 0, 14 ) ) { $query->set( 'meta_key', '_' . $orderby ); $query->set( 'orderby', 'meta_value_num' ); } } } /** * Make columns sortable. * * @param array $columns Table columns. * @return array */ function shop_order_sortable_columns( $columns ) { $custom = array(); foreach ( aiow_get_enabled_invoice_types_ids() as $invoice_type_id ) { $custom[ $invoice_type_id ] = 'aiow_invoicing_' . $invoice_type_id . '_number_id'; } return wp_parse_args( $custom, $columns ); } /** * Enqueue Scripts. */ function enqueue_scripts() { wp_enqueue_script( 'aiow-pdf-invoicing', aiow_plugin_url() . '/assets/js/invoicing.js', array(), false, true ); } /** * Add PDF invoices button CSS. */ function add_pdf_invoices_admin_actions_buttons_css() { echo '' . PHP_EOL; } /** * Add PDF invoices actions. * * @param array $actions PDF actions. * @param object $the_order Order object. * @return array */ function add_pdf_invoices_admin_actions( $actions, $the_order ) { $invoice_types = aiow_get_enabled_invoice_types(); foreach ( $invoice_types as $invoice_type ) { if ( aiow_is_invoice_created( aiow_get_order_id( $the_order ), $invoice_type['id'] ) ) { if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_admin_orders_view_btn', 'no' ) ) { // Document (View) button $query_args = array( 'order_id' => aiow_get_order_id( $the_order ), 'invoice_type_id' => $invoice_type['id'], 'get_invoice' => '1', ); if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_save_as_enabled', 'no' ) ) { $query_args['save_pdf_invoice'] = '1'; } $the_url = add_query_arg( $query_args, remove_query_arg( array ( 'create_invoice_for_order_id', 'delete_invoice_for_order_id' ) ) ); $the_name = __( 'View', 'all-in-one-wc' ) . ' ' . $invoice_type['title']; $the_action = 'view ' . $invoice_type['id']; $the_action_id = $invoice_type['id']; $actions[ $the_action_id ] = array( 'url' => $the_url, 'name' => $the_name, 'action' => $the_action ); } if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_admin_orders_delete_btn', 'yes' ) ) { // Delete button $query_args = array( 'delete_invoice_for_order_id' => aiow_get_order_id( $the_order ), 'invoice_type_id' => $invoice_type['id'] ); $the_url = add_query_arg( $query_args, remove_query_arg( 'create_invoice_for_order_id' ) ); $the_name = __( 'Delete', 'all-in-one-wc' ) . ' ' . $invoice_type['title']; $the_action = 'view ' . $invoice_type['id'] . '_' . 'delete' . ( ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_admin_orders_delete_btn_confirm', 'yes' ) ) ? ' aiow_need_confirmation' : '' ); $the_action_id = $invoice_type['id'] . '_' . 'delete'; $actions[ $the_action_id ] = array( 'url' => $the_url, 'name' => $the_name, 'action' => $the_action ); } } else { if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_admin_orders_create_btn', 'yes' ) ) { // Create button $query_args = array( 'create_invoice_for_order_id' => aiow_get_order_id( $the_order ), 'invoice_type_id' => $invoice_type['id'] ); $the_url = add_query_arg( $query_args, remove_query_arg( 'delete_invoice_for_order_id' ) ); $the_name = __( 'Create', 'all-in-one-wc' ) . ' ' . $invoice_type['title']; $the_action = 'view ' . $invoice_type['id'] . '_' . 'create' . ( ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_admin_orders_create_btn_confirm', 'yes' ) ) ? ' aiow_need_confirmation' : '' ); $the_action_id = $invoice_type['id'] . '_' . 'create'; $actions[ $the_action_id ] = array( 'url' => $the_url, 'name' => $the_name, 'action' => $the_action ); } } } return $actions; } /** * Add order column. * * @param array $columns Order columns. * @return array */ function add_order_column( $columns ) { unset( $columns['invoice'] ); unset( $columns['proforma_invoice'] ); unset( $columns['packing_slip'] ); unset( $columns['credit_note'] ); $invoice_types = aiow_get_enabled_invoice_types(); foreach ( $invoice_types as $invoice_type ) { if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_admin_orders_page_column', 'yes' ) ) { $columns[ $invoice_type['id'] ] = aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_admin_page_column_text', $invoice_type['title'] ); } } return $columns; } /** * Output custom columns for products. * * @param string $column order column. */ function render_order_columns( $column ) { $invoice_types_ids = aiow_get_enabled_invoice_types_ids(); if ( ! in_array( $column, $invoice_types_ids ) ) { return; } $order_id = get_the_ID(); $invoice_type_id = $column; $html = ''; if ( aiow_is_invoice_created( $order_id, $invoice_type_id ) ) { $the_invoice = aiow_get_invoice( $order_id, $invoice_type_id ); $the_number = $the_invoice->get_invoice_number(); // Document Link $query_args = array( 'order_id' => $order_id, 'invoice_type_id' => $invoice_type_id, 'get_invoice' => '1', ); if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type_id . '_save_as_enabled', 'no' ) ) { $query_args['save_pdf_invoice'] = '1'; } $html .= '' . $the_number . ''; } echo $html; } /** * Thank you page URL. * * @param int $order_id Order ID. */ function add_pdf_invoices_links_to_thankyou_page( $order_id ) { $invoice_types = aiow_get_enabled_invoice_types(); foreach ( $invoice_types as $invoice_type ) { if ( ! aiow_is_invoice_created( $order_id, $invoice_type['id'] ) ) { continue; } if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_enabled_on_thankyou_page', 'no' ) ) { $query_args = array( 'order_id' => $order_id, 'invoice_type_id' => $invoice_type['id'], 'get_invoice' => '1', ); if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_save_as_enabled', 'no' ) ) { $query_args['save_pdf_invoice'] = '1'; } if ( '' == ( $title = aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_thankyou_page_link_text', $invoice_type['title'] ) ) ) { $title = $invoice_type['title']; } echo str_replace( '%link%', '' . $title . '', get_option( 'aiow_invoicing_' . $invoice_type['id'] . '_thankyou_page_template', '

' . sprintf( __( 'Your %s:', 'all-in-one-wc' ), $invoice_type['title'] ) . ' %link%

' ) ); } } } /** * PDF action links. * * @param array $actions PDF action. * @param object $the_order Order data. * @return array */ function add_pdf_invoices_action_links( $actions, $the_order ) { $invoice_types = aiow_get_enabled_invoice_types(); foreach ( $invoice_types as $invoice_type ) { if ( ! aiow_is_invoice_created( aiow_get_order_id( $the_order ), $invoice_type['id'] ) ) { continue; } $my_account_option_name = 'aiow_invoicing_' . $invoice_type['id'] . '_enabled_for_customers'; if ( 'yes' === aiow_option( $my_account_option_name, 'no' ) ) { $the_action_id = $invoice_type['id']; $query_args = array( 'order_id' => aiow_get_order_id( $the_order ), 'invoice_type_id' => $invoice_type['id'], 'get_invoice' => '1', ); if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_save_as_enabled', 'no' ) ) { $query_args['save_pdf_invoice'] = '1'; } $the_url = add_query_arg( $query_args ); $the_name = aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_link_text' ); if ( '' == $the_name ) { $the_name = $invoice_type['title']; } $the_action = 'view ' . $invoice_type['id']; $actions[ $the_action_id ] = array( 'url' => $the_url, 'name' => $the_name, 'action' => $the_action ); } } return $actions; } /** * Add invoices metabox. */ function add_invoices_meta_box() { if ( 'yes' === aiow_option( 'aiow_invoicing_add_order_meta_box', 'yes' ) ) { add_meta_box( 'aiow-pdf-invoicing', '' . ' ' . __( 'PDF Invoices', 'all-in-one-wc' ), array( $this, 'create_invoices_meta_box' ), 'shop_order', 'side', 'default' ); } } /** * Register invoices metabox. */ function create_invoices_meta_box() { $_order = wc_get_order(); $order_id = aiow_get_order_id( $_order ); $invoice_types = aiow_get_enabled_invoice_types(); if ( empty( $invoice_types ) ) { echo '

' . __( 'You have no document types enabled.', 'all-in-one-wc' ) . '

'; } else { foreach ( $invoice_types as $invoice_type ) { $table_data = array(); $the_number = ''; $the_date = ''; if ( aiow_is_invoice_created( $order_id, $invoice_type['id'] ) ) { // "Document (View)" link $query_args = array( 'order_id' => $order_id, 'invoice_type_id' => $invoice_type['id'], 'get_invoice' => '1', ); $target = ( 'yes' === aiow_option( 'aiow_invoicing_order_meta_box_open_in_new_window', 'yes' ) ? ' target="_blank"' : '' ); if ( 'yes' === aiow_option( 'aiow_invoicing_' . $invoice_type['id'] . '_save_as_enabled', 'no' ) ) { $query_args['save_pdf_invoice'] = '1'; $target = ''; } $the_url = add_query_arg( $query_args, remove_query_arg( array ( 'create_invoice_for_order_id', 'delete_invoice_for_order_id' ) ) ); $the_name = __( 'View', 'all-in-one-wc' ); $the_invoice = aiow_get_invoice( $order_id, $invoice_type['id'] ); $the_number = ' [#' . $the_invoice->get_invoice_number() . ']'; $the_date = ' (' . date( 'Y-m-d', $the_invoice->get_invoice_date() ) . ')'; $view_link = '' . $the_name . ''; // "Delete" link $query_args = array( 'delete_invoice_for_order_id' => $order_id, 'invoice_type_id' => $invoice_type['id'] ); $the_url = add_query_arg( $query_args, remove_query_arg( 'create_invoice_for_order_id' ) ); $the_name = __( 'Delete', 'all-in-one-wc' ); $delete_link = '' . $the_name . ''; // Numbering & date $number_input = ''; if ( 'yes' === aiow_option( 'aiow_invoicing_add_order_meta_box_numbering', 'yes' ) ) { $number_option = 'aiow_invoicing_' . $invoice_type['id'] . '_number_id'; $date_option = 'aiow_invoicing_' . $invoice_type['id'] . '_date'; $number_input = '
' . '' . '' . ''; } // Actions $actions = array( $view_link . ' | ' . $delete_link . $number_input ); } else { // "Create" link $query_args = array( 'create_invoice_for_order_id' => $order_id, 'invoice_type_id' => $invoice_type['id'] ); $the_url = add_query_arg( $query_args, remove_query_arg( 'delete_invoice_for_order_id' ) ); $the_name = __( 'Create', 'all-in-one-wc' ); $actions = array( '' . $the_name . '' ); } $maybe_toolptip = ''; $_hooks = aiow_get_invoice_create_on( $invoice_type['id'] ); if ( in_array( 'woocommerce_order_partially_refunded_notification', $_hooks ) ) { $maybe_toolptip = wc_help_tip( __( 'In case of partial refund, you need to reload the page to see created document in this meta box.', 'all-in-one-wc' ), true ); } $table_data[] = array( '' . ' ' . $invoice_type['title'] . $the_number . $the_date . $maybe_toolptip ); $table_data[] = $actions; echo '

' . aiow_get_table_html( $table_data, array( 'table_class' => 'widefat striped' ) ) . '

'; } } } } }