Export all orders and add simpler counter to orders

Hello! I've noticed that there is no longer a mass order export function; it used to be you could export everything to a CSV. My question is whether there is still a function or method for exporting all the order data to CSV, and I have a related question about inserting a kind of counter function to the packing slips. Here is some background info:

For the past two years, with some coding tweaks, I would use the exported CSV to generate packing slips for customer pickup. Since the store has complicated, non-sequential order IDs (such as "25c6799badc5"), I also used to add a column to the CSV with my own order numbers (1001, 1002, 1003...), in order of the time the order was completed. This functionality is very important; it's the only way we can manage to quickly get orders out to about 700+ customers in a few hours. If there is still a way to get a CSV of all the orders, that would take care of everything - please let me know!

If there is not a way to export to CSV, what I would like to do is add a counter function that does two things - inserts numbers like 1001, 1002 to the filename, while counting up, and adding the same number to the packing slip and invoice titles in the templates for printing.

For example:

packing_730027338a87.pdf
would become:
packing_1001_730027338a87.pdf
and the next order would be packing_1002_[ID number].pdf, etc

I've found spots in the code where the pdf filename is generated, such as the below (found twice in class-mp-pdf-invoice.php):

if ( $type == self::PDF_INVOICE ) {
				$file = 'invoice_' . $order->post_name . '.pdf';
			} else {
				$file = 'packing_' . $order->post_name . '.pdf';
			}

and the title is generated in the templates invoice.php and packing.php - example:
<h3><?php _e( "Packing Slip", "mp" ) ?> #{{order_id}}</h3>

I realize this is a somewhat complicated post and request, and don't want to overstep my support requests. If any support staff can help me figure out where in the code to insert an incremental counter, and have that variable be global/accessible and used by the invoice/packing slip code, that would be wonderful. I may be able to figure this all out myself, but any advice would be appreciated! And again, all of this is only necessary if there isn't a way to export to CSV.
Thanks again,
Alex

  • Milan
    • WordPress Wizard

    Hello @Alex,

    I hope you are having a good day and thanks for the asking. :slight_smile:

    whether there is still a function or method for exporting all the order data to CSV,

    I am afraid at this moment, there isn't' any inbuilt method or function available for exporting all the order data to CSV.

    So we have to go with second option, But before we do so please tell me you want to change order id to incremental number or you want append/prepend incremental number before invoice and packing sleep name ?

    Looking forward to hearing back from you. :slight_smile:

    Cheers,
    Milan Savaliya.

  • Alex
    • Design Lord, Child of Thor

    Hello Milan,
    Thanks for your quick reply! If there isn't a CSV export method available, that is okay... adding these numbers will really help us manage the packing and pickup. What I would like to do is append the number, either before "packing_[$order->post_name]":
    "1003_packing_[$order->post_name].pdf"

    or in between "packing" and the ID number:
    "packing_1003_[$order->post_name].pdf"

    Doing the same on the invoice files. I'd like to keep the $order/post ID in the file name, rather than replace it, just to be able to check against orders in the store.

    Then, if we create this new counter variable, I can use it to insert the counter into the packing and invoice template files, too... say we call it $orderCount for example - I could do something like the following, putting the Order ID in a subheader:

    <h3><?php _e( "Packing Slip", "mp" ) ?> #{{orderCount}}</h3>
    <h4><?php _e( "Order ID", "mp" ) ?> #{{order_id}}</h4>

    Thanks again!!
    Alex

  • Milan
    • WordPress Wizard

    Hello @Alex,

    I hope you are having a good day and sorry for being late here.

    So finally you've to go with custom solution. There isn't any out of box way through which you can satisfy your requirement. So i've created one custom solution for you. Please inject below custom solution to your site and test is it working or not. I've tested on my sandbox site and its working fine.

    Notes:
    #1) Solution is divided into two parts. First code part you can inject into theme's functions.php file or better into child theme's functions.php file or you can use this nice plugin for future safety of code https://wordpress.org/plugins/code-snippets/

    #2) Another part you need to inject into plugin's core file /includes/addons/mp-pdf-invoice/class-mp-pdf-invoice.php file. Here please note that changes to core files won't be preserved when you update your plugin. So I strongly suggest you take backup of your code before you update your plugin.

    ======Solution======

    #Part 1:

    add_action( 'mp_order/new_order', 'handle_order_incremental', '', 1 );
    function handle_order_incremental( $orderObj ){
    
    	$key = 'increment_count_when_order_placed';
    	$defaultIncrementalOrderCount = 1002;
    
    	if( empty( get_post_meta( $orderObj->ID, $key, true ) ) ){
    
    		$currentIncrementalCount = get_option( 'wpmudev_current_incremental_count', $defaultIncrementalOrderCount );
    
    		add_post_meta( $orderObj->ID, $key, ++$currentIncrementalCount, true );
    
    		update_option( 'wpmudev_current_incremental_count', $currentIncrementalCount );
    
    	}
    }

    In above code snippet you need to change $defaultIncrementalOrderCount to number from where you want to start order counting.

    increment_count_when_order_placed is key used to put meta data to order post about current order count. Its value will let you know what was order counting when order was placed.

    #Part 2:
    Go to /includes/addons/mp-pdf-invoice/class-mp-pdf-invoice.php file and replace code

    if ( $type == self::PDF_INVOICE ) {
    				$file = 'invoice_' . $order->post_name . '.pdf';
    			} else {
    				$file = 'packing_' . $order->post_name . '.pdf';
    			}

    with this one

    $orderCountAttimeOfOrder = get_post_meta( $order->ID, 'increment_count_when_order_placed', true );
    
    			$prependString = '';
    			if( !empty( $orderCountAttimeOfOrder ) ){
    				$prependString = $orderCountAttimeOfOrder . '_';
    			}
    
    			if ( $type == self::PDF_INVOICE ) {
    				$file = $prependString . 'invoice_' . $order->post_name . '.pdf';
    			} else {
    				$file = $prependString . 'packing_' . $order->post_name . '.pdf';
    			}

    Important Note: Above solution will only work for order which will be placed after you setup custom solution. And you must have selected "Donwload the PDF" option for invoice generation settings page. ( Store Setttings > Addons > Invoice > settings )

    I hope this helps you.

    Please let me know if there is anything else I can help you with or you have quries with this one.

    Cheers,
    Milan Savaliya.

  • Alex
    • Design Lord, Child of Thor

    Hello Milan,
    Thank you for working out this solution! The solution doesn't work yet, as far as changing the file name, but it doesn't cause any site-crashing errors either, which is good! I'd like the file names to universally change - whether it's the one the customer gets attached in their email, or the packing slips stored in wp-content/uploads/mp_pdf_invoice.

    Two questions:
    1 - There are actually two locations with "if ( $type == self::stuck_out_tongue:DF_INVOICE ) {..." code snippet. I assumed I should replace them both (while only declaring the first line, "$orderCountAttimeOfOrder = get_post_meta( $order->ID,..." once). Is that correct?

    2 - I also wanted to ask: can I use this same solution and variable, modified for details, in the template files to change the titles? Unlike the file names, the templates call up the order ID with php_e:

    <h3><?php _e( "Packing Slip", "mp" ) ?> #{{order_id}}</h3>
    Thanks!
    Alex

  • Alex
    • Design Lord, Child of Thor

    Actually, it looks like functions.php in my child theme wasn't properly replaced when I wrote the above post, so that's why nothing happened. The function causes a fatal error and the site won't load because of the error:

    Fatal error: Can't use function return value in write context in /home/content/a/n/d/andrujohnson/html/alpha/wp-content/themes/twentytwelve-child/functions.php on line 262

    The line 262 is this line in your code addition:
    	if( empty( get_post_meta( $orderObj->ID, $key, true ) ) ){

    I just have the code added into functions.php in my child theme files, right before the php closes, as below:

    //Marketpress - function for adding incremental order counter
    add_action( 'mp_order/new_order', 'handle_order_incremental', '', 1 );
    function handle_order_incremental( $orderObj ){
    	$key = 'increment_count_when_order_placed';
    	$defaultIncrementalOrderCount = 1002;
    	if( empty( get_post_meta( $orderObj->ID, $key, true ) ) ){
    		$currentIncrementalCount = get_option( 'wpmudev_current_incremental_count', $defaultIncrementalOrderCount );
    		add_post_meta( $orderObj->ID, $key, ++$currentIncrementalCount, true );
    		update_option( 'wpmudev_current_incremental_count', $currentIncrementalCount );
    	}
    }
    
    ?>

    I will comment out the code for now, and give a support pass, if that's any help. Thanks again!

  • Milan
    • WordPress Wizard

    Hello @Alex,

    Hope you are doing well. :slight_smile:

    That's because prior to php version 5.5 function empty() triggers fatal error when checking it for functions rather than variable.

    So Can you replace few lines of code which you've pasted in your functions.php file ?

    if( empty( get_post_meta( $orderObj->ID, $key, true ) ) ){

    with this one ?

    $incrementalCountWhenOrderPlaced = get_post_meta( $orderObj->ID, $key, true  );
    if( empty( $incrementalCountWhenOrderPlaced ) ){

    Let me know if it solves your fatal error or not ?

    Cheers,
    Milan Savaliya.

  • Alex
    • Design Lord, Child of Thor

    Hello Milan,
    That solved the error! And now only the invoice you download from the "Order Status" page correctly appends an incrementing number in the beginning.

    However, as I wrote above, I'd like the file names to universally change - whether it's the one the customer gets attached in their email, or the packing slips stored in wp-content/uploads/mp_pdf_invoice. The ones stored in uploads are actually my biggest priority because it's those packing slips I want to batch.

    I am also looking to grab this variable $currentIncrementalCount and drop it into these lines in the templates (invoice.php, packing.php):

    <h3><?php _e( "Packing Slip", "mp" ) ?> #{{order_id}}</h3>
    
    <h3><?php _e( "Invoice", "mp" ) ?> #{{order_id}}</h3>

    but I'm not sure of the correct syntax. Would it just be:
    <h3><?php _e( "Packing Slip", "mp" ) ?> #{{currentIncrementalCount}}</h3>
    Thank you again for all your help with this!
    Alex

  • Alex
    • Design Lord, Child of Thor

    Oops, my other post disappeared ... I figured out how to print the Order ID on all packing slips and invoices! I borrowed your above code and made the below adaptation in packing.php and invoice.php (changing just the "Invoice/Packing Slip" text in each). Replace:
    <h3><?php _e( &quot;Order ID&quot;, &quot;mp&quot; ) ?> #{{order_id}}</h3>
    with:

    <h3><?php
    		$orderCountAttimeOfOrder = get_post_meta( $order->ID, 'increment_count_when_order_placed', true );
    
    		$orderInsert = '';
    		if( !empty( $orderCountAttimeOfOrder ) ){
    			$orderInsert = '#'. $orderCountAttimeOfOrder ;
    		}
    			echo 'Packing Slip '. $orderInsert; ?>
    		</h3>
            <h4><?php _e( &quot;Order ID&quot;, &quot;mp&quot; ) ?> #{{order_id}}</h4>

    Which prints the Order ID in a subheader, as in the screenshot below. Now the only thing left is adding the Order ID to all the various pdf files that get sent/stored.

  • Alex
    • Design Lord, Child of Thor

    Hi Milan,
    I was curious if you had time to review my question about getting all invoices and packing slip files to prepend with the order counter. I know you handle many many support requests! My sale starts on the 20th, so I just want to be sure I am storing invoices and packing slips with the order number in wp-content/uploads/mp_pdf_invoice. Right now the implemented code only affects the downloaded invoice.

    As noted above, I figured out how to print the same number in the templates, so that's all taken care of.
    Thanks!
    Alex

  • Milan
    • WordPress Wizard

    Hello @Alex,

    I hope you are having a good day and sorry for being late here.

    I confirmed with developer that for that either you can customize default template provided by Marketpress or create new one. For that you can go to wp-admin/admin.php?page=store-settings-addons&addon=MP_PDF_Invoice_Addon to customize templates preferences.

    The recommendation here is to create a new template so you don’t lost the changes if Marketpress updates the PDF add-on. you can go to marketpress/includes/addons/mp-pdf-invoice/templaters. Copy default folder and paste it under "/templates" directory of plugin. Rename it to a different folder name that'll create new template for you.

    Then inside you have created template folder you'll see two files named "invoice.php" and "packing.php". These file's names represents tempalte which being sent to customer. Modify it according to your need.

    Here one thing I want to point out is that changes to core files won't be preserved when you update plugin so make sure you take backup of your changes before update.

    I hope this helps you.

    Cheers,
    Milan Savaliya.

  • Alex
    • Design Lord, Child of Thor

    Hi Milan,
    I think you misunderstood - I've already taken care of the templates, invoice.php and packing.php. Thank you, though, and no problem about the late reply. My only remaining issue is getting all the files to prepend with the 4-digit number.

    Right now, the only ways it prepends the number is when the customer hits the button on the order status page, or when you go to a customer's order in the backend and do the same. My issue is that the copies stored on the server in wp-content/uploads/mp_pdf_invoice, and the ones sent to email, do not have changed file names yet.
    Thanks,
    Alex

  • Milan
    • WordPress Wizard

    Hello @Alex,

    Hope you are doing well.

    Thanks for clarifying issue and sorry that I misunderstood your question.

    As I said you before as Important note you can't actually change those files name with this solution. Solution will only work for those order which customer will place after you setup solution. There isn't any way you can change files name of previous orders. You had to do manually as there isn't any out of box way to do so. I am sorry but you had to go with manual way for this.

    Let me know if you need help with anything else.

    Cheers,
    Milan Savaliya.

  • Milan
    • WordPress Wizard

    Hello @Alex,

    Hope you are doing well and thanks for clarifying your concern again.

    I am sorry that I overlooked your explanations. :slight_frown:

    My issue is that the copies stored on the server in wp-content/uploads/mp_pdf_invoice, and the ones sent to email, do not have changed file names yet.

    I've tested solution on my sandbox site. Everything is working fine as it should. Copies are stored with number prepended in /wp-content/uploads/mp_pdf_invoice/ folder. I've attached screenshot for your confirmation. Also I've test email which sent to customer after they order something, I found that files which are attached has prepended order count number.

    So may be you have made below change only once in /includes/addons/mp-pdf-invoice/class-mp-pdf-invoice.php file.

    if ( $type == self::PDF_INVOICE ) {
    				$file = 'invoice_' . $order->post_name . '.pdf';
    			} else {
    				$file = 'packing_' . $order->post_name . '.pdf';
    			}

    with this one

    $orderCountAttimeOfOrder = get_post_meta( $order->ID, 'increment_count_when_order_placed', true );
    
    			$prependString = '';
    			if( !empty( $orderCountAttimeOfOrder ) ){
    				$prependString = $orderCountAttimeOfOrder . '_';
    			}
    
    			if ( $type == self::PDF_INVOICE ) {
    				$file = $prependString . 'invoice_' . $order->post_name . '.pdf';
    			} else {
    				$file = $prependString . 'packing_' . $order->post_name . '.pdf';
    			}

    You'll need to replace mentioned code at 2 places in file /includes/addons/mp-pdf-invoice/class-mp-pdf-invoice.php. If you only replaced it for once please find another entry and replace that too.

    Kind Regards,
    Milan Savaliya.

  • Alex
    • Design Lord, Child of Thor

    Hi Milan,
    Thank you for your quick reply, and no problem :slight_smile:. I had included the replacement twice, but foolishly commented out the line where it defined $orderCountAttimeOfOrder, thinking I only needed to declare it once:

    1 - There are actually two locations with "if ( $type == self::stuck_out_tongue:DF_INVOICE ) {..." code snippet. I assumed I should replace them both (while only declaring the first line, "$orderCountAttimeOfOrder = get_post_meta( $order->ID,..." once). Is that correct?

    I removed the // and all of a sudden, everything gets saved correctly! Including a packing slip in the uploads folder, and all the emails. So that problem was all of my own doing, but now it's fixed.

    :slight_smile: :slight_smile: :slight_smile:

    Thank you again, and thanks for your patience - sorry for the trouble my comment-out caused. This issue is now solved/resolved!
    Alex

  • Alex
    • Design Lord, Child of Thor

    Huh - seems some of my above posts about resolving the correct issue have been removed??

    I just wanted to chime back in on this - I wouldn't recommend this fix quite as it's written. Many orders, probably about 50 or more out of 750, came out with the same 4-digit number, so I had to go through all the printed packing slips with a sharpie and write in "A, B, C" on the duplicate order numbers. Perhaps if it was rewritten so that the plugin checked and reserved a number each time an order was actually completed, that would fix the issue. Anyhow, no worries, I probably won't be using this fix again; I just wanted to let everybody else know. Thanks again Milan for your help here.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.