Last active
January 30, 2018 22:42
-
-
Save grantalltodavid/5d8961170cc89a41eb22f82d6d30e8a8 to your computer and use it in GitHub Desktop.
Sliced Invoices Deposit Invoices Extension: Auto convert accepted quotes into deposit invoices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'sliced_client_accepted_quote', 'sliced_auto_create_deposit' ); | |
function sliced_auto_create_deposit( $id ) { | |
global $wpdb; | |
$amount = '40'; // 40% deposit, adjust if needed | |
$type = 'percentage'; // 'percentage' or 'amount' | |
// **** don't edit below here **** | |
update_post_meta( $id, '_sliced_invoice_deposit', array( 'amount' => $amount, 'type' => $type ) ); | |
if ( function_exists( 'sliced_get_accepted_payment_methods' ) ) { | |
$payment = sliced_get_accepted_payment_methods(); | |
update_post_meta( $id, '_sliced_payment_methods', array_keys($payment) ); | |
} | |
$parent_invoice = get_post( $id ); | |
$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, | |
) ); | |
$new_invoice_id = wp_insert_post( $args ); | |
$taxonomies = get_object_taxonomies($parent_invoice->post_type); | |
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); | |
} | |
$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); | |
} | |
$number = sliced_get_invoice_number( $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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment