When quote converts to invoice, automatically send deposit invoice.
We need to be able to send our clients our estimates (which we can currently.) They then need to be able to review it, accept the quote, and then pay the deposit. Currently, the quote can automatically convert to invoice and then send the invoice but our clients only owe us a deposit to begin work, not the entire amount. I want to be able to set the plugin to automatically issue a deposit invoice as soon as the quote is accepted.
3 Comments. Leave new
Select “convert to invoice” from the Quote settings and add this code to your functions.php file:
function create_deposit( $id ) {
global $wpdb;
// Sanitize the user input and Update the meta field
$amount = sanitize_text_field( “50” );
$type = sanitize_text_field( “percentage” );
update_post_meta( $id, ‘_sliced_invoice_deposit’, array( ‘amount’ => $amount, ‘type’ => $type ) );
// get the parent invoice post object so we can copy the data
$parent_invoice = get_post( $id );
// Arguments for the new invoice
$args = apply_filters( ‘sliced_deposit_invoice_args’, array(
‘post_title’ => __( ‘Deposit -‘, ‘sliced-invoices-deposit’ ) . ‘ ‘ . $parent_invoice->post_title,
‘post_content’ => $parent_invoice->post_content,
‘post_status’ => ‘publish’,
‘post_type’ => ‘sliced_invoice’,
‘post_parent’ => $parent_invoice->ID,
‘post_password’ => $parent_invoice->post_password,
‘post_date’ => $parent_invoice->post_date,
) );
// Insert the new child/deposit invoice into the database
$new_invoice_id = wp_insert_post( $args );
/*
* get all current post terms ad set them to the new post draft
*/
$taxonomies = get_object_taxonomies($parent_invoice->post_type); // returns array of taxonomy names for post type, ex array(“category”, “post_tag”);
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($parent_invoice->ID, $taxonomy, array(‘fields’ => ‘slugs’));
wp_set_object_terms($new_invoice_id, $post_terms, $taxonomy, false);
}
/*
* duplicate all post meta
*/
$post_meta_infos = $wpdb->get_results(“SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$parent_invoice->ID”);
if (count($post_meta_infos)!=0) {
$sql_query = “INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) “;
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= “SELECT $new_invoice_id, ‘$meta_key’, ‘$meta_value'”;
}
$sql_query.= implode(” UNION ALL “, $sql_query_sel);
$wpdb->query($sql_query);
}
// update the invoice number on the deposit with a suffix
$number = sliced_get_invoice_number( $parent_invoice->ID );
update_post_meta( $new_invoice_id, ‘_sliced_invoice_number’, $number . ‘-1’ );
update_post_meta( $new_invoice_id, ‘_sliced_deposit_parent’, $id );
update_post_meta( $id, ‘_sliced_deposit_child’, $new_invoice_id );
$redirect = add_query_arg( array( ‘p’ => $new_invoice_id ), home_url() );
wp_redirect( $redirect );
exit;
}
add_action( ‘sliced_client_accepted_quote’, ‘create_deposit’, 10, 1 );
This just crashed my site. Is there a reviewed version?
You can make all new invoices automatically have a 50% down payment but it requires you to edit the partial payments plugin source files. This method will have to be repeated every time you update the partial payments plugin.
Step one
Locate the sliced-invoices-partial-payments.php file. It will be located in this directory
public_html/wp-content/plugins/sliced-invoices-partial-payments
Step Two
Update the plugin code to the following to create the default values for the partial payment settings.
This starts on line 273
$info->add_field( array(
‘name’ => ”,
‘desc’ => __( ‘Show “Minimum Payment” option’, ‘sliced-invoices-partial-payments’ ),
‘id’ => ‘_sliced_partial_payments_minimum_enable’,
‘type’ => ‘checkbox’,
‘default’ => ‘TRUE’, //turns partial payments for every new invoice
) );
$info->add_field( array(
‘name’ => __( ‘Minimum Payment Amount (required)’, ‘sliced-invoices-partial-payments’ ),
‘desc’ => ”,
‘id’ => ‘_sliced_partial_payments_minimum_amount’,
‘type’ => ‘text’,
‘default’ => sliced_get_invoice_total_due_raw($id)/2, //gets raw total and divides it by 2
) );