Doesn't clone WP Invoice invoices correctly..
-
I stopped using this to clone invoices I created with WP Invoice because the cloned invoices weren’t showing up in the WP Invoice “All Invoices” list. I could see the cloned invoices in the wp_posts table, but not in the WP Invoices “All Invoices” list..
Today I finally had some time to figure out why..
WP Invoice sets all NEW invoices to the post status of “active”. This plugin sets the post status of cloned pages/posts to “draft”. If an invoice’s post status is set to “draft” as this plugin does, then the invoice won’t appear in the WP Invoice “All Invoice”.
Not sure how to address this.. Not sure if this requires a fix to this plugin or a fix to the WP Invoice plugin.. I suspect that the fix needs to come from the WP Invoice plugin..
In the meantime I’ve had to create a custom function to clone invoices as a workaround. For anyone who needs this same functionality, I’m sharing my code.. You need to add this to your theme’s functions.php file.. The source I based this code on comes from here: http://rudrastyh.com/wordpress/duplicate-post.html. I’ve modified this code from the original so that it ONLY works for WP Invoices.. (I still use the Duplicate Posts plugin to clone pages and posts)
/* * Function creates invoice duplicate and redirects to the "All Invoices" list screen */ function rd_duplicate_wpinvoice(){ global $wpdb; if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'rd_duplicate_wpinvoice' == $_REQUEST['action'] ) ) ) { wp_die('No post to duplicate has been supplied!'); } /* * get the original invoice post id */ $post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']); /* * and all the original invoice post data then */ $post = get_post( $post_id ); /* * if you don't want current user to be the new invoice author, * then change next couple of lines to this: $new_post_author = $post->post_author; */ $current_user = wp_get_current_user(); $new_post_author = $current_user->ID; /* * if invoice data exists, create the invoice duplicate */ if (isset( $post ) && $post != null) { /* * new post data array */ $args = array( 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_author' => $new_post_author, 'post_content' => $post->post_content, 'post_excerpt' => $post->post_excerpt, 'post_name' => $post->post_name, 'post_parent' => $post->post_parent, 'post_password' => $post->post_password, 'post_status' => 'active', //default status for new WP Invoice invoice 'post_title' => $post->post_title, 'post_type' => $post->post_type, 'to_ping' => $post->to_ping, 'menu_order' => $post->menu_order ); /* * insert the post by wp_insert_post() function */ $new_post_id = wp_insert_post( $args ); /* * get all current post terms for the invoice and set them to the new invoice */ $taxonomies = get_object_taxonomies($post->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($post_id, $taxonomy, array('fields' => 'slugs')); wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false); } /* * duplicate the invoice post meta */ $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_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_post_id, '$meta_key', '$meta_value'"; } $sql_query.= implode(" UNION ALL ", $sql_query_sel); $wpdb->query($sql_query); } /* * finally, redirect to the "All Posts" screen */ // wp_redirect( admin_url( 'admin.php?page=wpi_page_manage_invoice&wpi[existing_invoice][invoice_id]=' . $new_post_id ) ); // wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) ); wp_redirect( admin_url( 'admin.php?page=wpi_main' ) ); exit; } else { wp_die('Post creation failed, could not find original invoice: ' . $post_id); } } add_action( 'admin_action_rd_duplicate_wpinvoice', 'rd_duplicate_wpinvoice' ); /* * Add the duplicate link to action list for wpi_object_row_actions */ function rd_duplicate_post_link( $actions, $post ) { if (current_user_can('edit_posts')) { $actions['duplicate'] = '<a href="admin.php?action=rd_duplicate_wpinvoice&post=' . $post->ID . '" title="Duplicate this invoice (DO NOT USE THIS LINK TO DUPLICATE POSTS or PAGES!)" rel="permalink">Duplicate Invoice</a>'; } return $actions; } add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 ); add_filter( 'page_row_actions', 'rd_duplicate_post_link', 10, 2 ); add_filter( 'wpi_object_row_actions', 'rd_duplicate_post_link', 10, 2 );
The topic ‘Doesn't clone WP Invoice invoices correctly..’ is closed to new replies.