• Resolved robinjoshua

    (@robinjoshua)


    Hello,

    In the settings of the plugin it is possible to show the invoice number in the order list. Is it also possible to add a column with the invoice date?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @robinjoshua,

    In order to add a new column in the order list to show the invoice date, please add this code snippet to your site:

    /**
     * Add a column in the order list to show the invoice date 
     */
    add_filter( 'manage_edit-shop_order_columns', 'wpo_wcpdf_invoice_date_column', 9999 );
    function wpo_wcpdf_invoice_date_column( $columns ) {
    	$insert_after = 'pdf_invoice_number';
    	$position = array_search( $insert_after, array_keys( $columns ) ) + 1;
    	// put the column after the Status/Invoice number column
    	return array_slice( $columns, 0, $position, true )
    	       + array( 'pdf_invoice_date' => __( 'Invoice Date', 'woocommerce-pdf-invoices-packing-slips' ) )
    	       + array_slice( $columns, $position, null, true ) ;
    }
    add_action( 'manage_shop_order_posts_custom_column', 'wpo_wcpdf_invoice_date_column_data', 10, 2 );
    function wpo_wcpdf_invoice_date_column_data( $column, $post_id ) {
    	if ( $column == 'pdf_invoice_date' && $order = wc_get_order( $post_id ) ) {
    		add_filter( 'wpo_wcpdf_document_store_settings', '__return_false', 99992 );
    		$invoice = wcpdf_get_invoice( $order );
    		if ( $invoice && $invoice_date = $invoice->get_date() ) {
    			echo $invoice_date->date_i18n( wcpdf_date_format( $invoice, 'invoice_date' ) );
    		}
    		remove_filter( 'wpo_wcpdf_document_store_settings', '__return_false', 99992 );
    	}
    }

    If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters

    Let me know if it works!

    Thread Starter robinjoshua

    (@robinjoshua)

    You are the man! Thank you.

    Thread Starter robinjoshua

    (@robinjoshua)

    One additional question. Do you also know how to add a sort functionality? So that I can sort the orders from latest invoice dates to older ones?

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Try to replace the code snippet above for this one:

    /**
     * Add a column in the order list to show the invoice date
     *
     * See: https://www.ads-software.com/support/topic/invoice-date-column-in-order-list/#post-14245482
     */
    add_filter( 'manage_edit-shop_order_columns', 'wpo_wcpdf_invoice_date_column', 9999 );
    function wpo_wcpdf_invoice_date_column( $columns ) {
    	$insert_after = 'pdf_invoice_number';
    	$position = array_search( $insert_after, array_keys( $columns ) ) + 1;
    	// put the column after the Status/Invoice number column
    	return array_slice( $columns, 0, $position, true )
    	       + array( 'pdf_invoice_date' => __( 'Invoice Date', 'woocommerce-pdf-invoices-packing-slips' ) )
    	       + array_slice( $columns, $position, null, true ) ;
    }
    add_action( 'manage_shop_order_posts_custom_column', 'wpo_wcpdf_invoice_date_column_data', 10, 2 );
    function wpo_wcpdf_invoice_date_column_data( $column, $post_id ) {
    	if ( $column == 'pdf_invoice_date' && $order = wc_get_order( $post_id ) ) {
    		add_filter( 'wpo_wcpdf_document_store_settings', '__return_false', 99992 );
    		$invoice = wcpdf_get_invoice( $order );
    		if ( $invoice && $invoice_date = $invoice->get_date() ) {
    			echo $invoice_date->date_i18n( wcpdf_date_format( $invoice, 'invoice_date' ) );
    		}
    		remove_filter( 'wpo_wcpdf_document_store_settings', '__return_false', 99992 );
    	}
    }
    add_filter( 'manage_edit-shop_order_sortable_columns', 'invoice_date_column_sortable');
    function invoice_date_column_sortable( $columns ) {
      $columns['pdf_invoice_date'] = 'pdf_invoice_date';
      return $columns;
    }
    add_filter( 'request', 'request_query_sort_by_invoice_date' );
    function request_query_sort_by_invoice_date( $query_vars ) {
      global $typenow;
      
      if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ), true ) ) {
        if ( isset( $query_vars['orderby'] ) ) {
          if ( 'pdf_invoice_date' === $query_vars['orderby'] ) {
            $query_vars = array_merge( $query_vars, array(
              'meta_key'  => '_wcpdf_invoice_date',
              'orderby'   => apply_filters( 'wpo_wcpdf_invoice_date_column_orderby', 'meta_value' ),
              ) );
            }
          }
        }
      return $query_vars;
    }

    Hope it helps! ??

    Thread Starter robinjoshua

    (@robinjoshua)

    It works, thank you Yordan!

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hello @robinjoshua,

    I’m glad to know it works!

    If you don’t mind and have the time, do you think you could leave us a review?

    Thanks in advance and all the best with your store!

    Thread Starter robinjoshua

    (@robinjoshua)

    That’s the least I could do. ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Invoice date column in order list’ is closed to new replies.