diff --git a/assets/js/admin-script.js b/assets/js/admin-script.js index f7feece13..31d4bf339 100644 --- a/assets/js/admin-script.js +++ b/assets/js/admin-script.js @@ -105,6 +105,20 @@ jQuery( function( $ ) { 'delay': 200 } ); + function toggle_due_date_custom_days_field() { + const due_date_value = $( '#due_date' ).val(); + + if ( 'custom' === due_date_value ) { + $( '#due_date_custom_days' ).closest( 'tr' ).show(); + } else { + $( '#due_date_custom_days' ).closest( 'tr' ).hide(); + } + } + + toggle_due_date_custom_days_field(); + + $( '#due_date' ).change( toggle_due_date_custom_days_field ); + //----------> Preview <----------// // objects let $previewWrapper = $( '#wpo-wcpdf-preview-wrapper' ); diff --git a/includes/documents/class-wcpdf-invoice.php b/includes/documents/class-wcpdf-invoice.php index 187920173..095899730 100644 --- a/includes/documents/class-wcpdf-invoice.php +++ b/includes/documents/class-wcpdf-invoice.php @@ -506,6 +506,38 @@ public function get_pdf_settings_fields( $option_name ) { 'description' => sprintf(__( "Disable document when the order total is %s", 'woocommerce-pdf-invoices-packing-slips' ), function_exists('wc_price') ? wc_price( 0 ) : 0 ), ) ), + array( + 'type' => 'setting', + 'id' => 'due_date', + 'title' => __( 'Due date', 'woocommerce-pdf-invoices-packing-slips' ), + 'callback' => 'select', + 'section' => $this->type, + 'args' => array( + 'option_name' => $option_name, + 'id' => 'due_date', + 'options' => apply_filters( 'wpo_wcpdf_due_date_options', array( + // For predefined dates, use this format: "{number_of_days}_day(s)". + '0_day' => __( 'None', 'wpo_wcpdf_pro' ), + '1_day' => __( '1 day', 'wpo_wcpdf_pro' ), + '7_days' => __( '7 days', 'wpo_wcpdf_pro' ), + '30_days' => __( '30 days', 'wpo_wcpdf_pro' ), + 'custom' => __( 'Custom date', 'wpo_wcpdf_pro' ), + ) ), + 'description' => __( 'Select a due date to display in the invoice.', 'woocommerce-pdf-invoices-packing-slips' ), + ) + ), + array( + 'type' => 'setting', + 'id' => 'due_date_custom_days', + 'title' => __( 'Custom Due Date (Days)', 'woocommerce-pdf-invoices-packing-slips' ), + 'callback' => 'text_input', + 'section' => $this->type, + 'args' => array( + 'option_name' => $option_name, + 'id' => 'due_date_custom_days', + 'description' => __( 'Enter the number of days for the due date.', 'woocommerce-pdf-invoices-packing-slips' ), + ) + ), array( 'type' => 'setting', 'id' => 'mark_printed', @@ -650,6 +682,27 @@ public function get_date_title() { return apply_filters( "wpo_wcpdf_{$this->slug}_date_title", $date_title, $this ); } + /** + * @param \WC_Order|\WC_Order_Refund $order + * + * @return false|string + */ + public function get_due_date( $order ) { + if ( empty( $this->settings['due_date'] ) ) { + return false; + } + + $due_days = ( 'custom' === $this->settings['due_date'] ) ? intval( $this->settings['due_date_custom_days'] ?? 0 ) : absint( strtok( $this->settings['due_date'], '_' ) ); + + if ( 0 >= $due_days ) { + return false; + } + + $due_date_datetime = $order->get_date_created()->modify( "+$due_days days" ); + + return apply_filters( "wpo_wcpdf_{$this->slug}_invoice_due_date", date( 'Y-m-d', $due_date_datetime->getTimestamp() ), $due_date_datetime->getTimestamp(), $order ); + } + } endif; // class_exists diff --git a/templates/Simple/invoice.php b/templates/Simple/invoice.php index 6c9b6fce3..5cf7cfa05 100644 --- a/templates/Simple/invoice.php +++ b/templates/Simple/invoice.php @@ -88,6 +88,12 @@