Skip to content

Commit

Permalink
Add due date feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamadNateqi committed Oct 26, 2023
1 parent be25d78 commit fa382ed
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
14 changes: 14 additions & 0 deletions assets/js/admin-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down
53 changes: 53 additions & 0 deletions includes/documents/class-wcpdf-invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions templates/Simple/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
<td><?php echo $payment_method; ?></td>
</tr>
<?php endif; ?>
<?php if ( $due_date = $this->get_due_date( $this->order ) ) : ?>
<tr class="due-date">
<th><?php _e( 'Due Date:', 'woocommerce-pdf-invoices-packing-slips' ); ?></th>
<td><?php echo $due_date; ?></td>
</tr>
<?php endif; ?>
<?php do_action( 'wpo_wcpdf_after_order_data', $this->get_type(), $this->order ); ?>
</table>
</td>
Expand Down

0 comments on commit fa382ed

Please sign in to comment.