• Resolved DivaVocals

    (@divavocals)


    I’ve had to create a custom function to clone invoices as the Duplicate Posts plugin doesn’t work with WP Invoice.

    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 );

    https://wordpress.org/plugins/wp-invoice/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter DivaVocals

    (@divavocals)

    Wanted to update that this isn’t working.. Gotta do some tweaking to it.. It’s duplicates the invoice, BUT the invoice hash still points to the parent invoice.. Gotta do some tweaking to it..

    It REALLY would be nice if WP Invoice offered this feature..

    Plugin Contributor Anton Korotkoff

    (@anton-korotkoff)

    Hello,

    what is the goal of duplicating invoices?

    Thread Starter DivaVocals

    (@divavocals)

    Hello Anton..

    Glad you asked.. It’s very simple..
    I may provide services to customers on a regular but NOT recurring basis. Being able to duplicate a previous invoice would make the process of creating these invoices easier.. I have a personal chef client who has routinely asked me to find a way for her to do this for the VERY SAME reasons..

    Here’s another scenario:
    I write up a detailed proposal/quote which I eventually turn into an invoice.. I may provide the very same/similar service for a different client,and being able to duplicate a previous invoice would make the process of creating the new invoice easier.

    Bottomline duplication would make it easy to spin up similar/same invoices/quotes.

    Plugin Contributor Anton Korotkoff

    (@anton-korotkoff)

    Hello,

    we’ll review this function. I think more convenient way of doing this exists. Maybe it will appear in some of the next versions.

    Thanks.

    Thread Starter DivaVocals

    (@divavocals)

    Anton, that would be awesome!! Ever since you resolved the issues with the truncated body text, this is my GO TO, CAN’T LIVE WITHOUT plugin..

    Plugin Contributor Anton Korotkoff

    (@anton-korotkoff)

    Actually, regarding copying invoices – this may be an option for you http://screencast.com/t/ZfQCrAcT What you think?

    Thread Starter DivaVocals

    (@divavocals)

    **blush**

    Well Anton.. Color me corrected.. I didn’t even KNOW about this feature.. and it works perfectly.. Let me go mark this as “resolved”, and show my client how to use the feature..

    Sorry to bother you.. 🙂 carry on..

    Plugin Contributor Anton Korotkoff

    (@anton-korotkoff)

    No problem =)

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[How-To] Function to Duplicate Invoices’ is closed to new replies.