Can I change the data configuration on the Order Info export to CSV?

Hi,

Is there any way to change the formatting of the Order Info export to CSV so that all the Item info isn't contained in one cell? The SKU, Item name, color, and quantity, for all the items in an order are lumped together in one cell with the color and quantity separated by colons. This makes it very difficult to parse that data into, say Packing Lists, which is what I am doing by taking that data into Excel to generate packing lists because Marketpress doesn't do Packing Lists - hint, hint... :slight_smile:

Anyway, is there a way to have that data present itself in rows and columns rather than all in one cell?

Thanks,

Sherry

  • Rahul Verma

    Hello Sherry,

    Yeah It's possible by applying some coding changes in export_orders_csv function defined in markertpress.php file.

    If you want me to do these modification directly your site, Please give me FTP access.

    Please send info through our contact form: https://premium.wpmudev.org/contact/

    - Choose "I have a different question"
    - Include my name in the subject "Sandeep Kumar"
    - Include the URL of this post in your message so that I may track this issue better
    - Include any relevant login information (Wordpress admin info username + password )
    - Include FTP Details

    Best Regards,
    Sandeep Kumar

  • Rahul Verma

    Hello Sherry,

    If you'd like to try it, Please go with following steps to implement this functionality.

    Step 1 : To make it a hassle free, We wrote a new function which you just need to paste in wp-content/plugins/markerpress/marketpress.php

    function export_orders_csv_per_items() {
    		global $wpdb;
    		//check permissions
    		$post_type_object = get_post_type_object('mp_order');
    		if ( !current_user_can($post_type_object->cap->edit_posts) )
    			wp_die(__('Cheatin’ uh?'));
    
    		$query = "SELECT ID, post_title, post_date, post_status FROM {$wpdb->posts} WHERE post_type = 'mp_order'";
    
    		if (isset($_POST['order_status']) && $_POST['order_status'] != 'all')
    			$query .= $wpdb->prepare(' AND post_status = %s', $_POST['order_status']);
    
    		// If a month is specified in the querystring, load that month
    		if ( isset($_POST['m']) && $_POST['m'] > 0 ) {
    			$_POST['m'] = '' . preg_replace('|[^0-9]|', '', $_POST['m']);
    			$query .= " AND YEAR($wpdb->posts.post_date)=" . substr($_POST['m'], 0, 4);
    			if ( strlen($_POST['m']) > 5 )
    				$query .= " AND MONTH($wpdb->posts.post_date)=" . substr($_POST['m'], 4, 2);
    			if ( strlen($_POST['m']) > 7 )
    				$query .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($_POST['m'], 6, 2);
    			if ( strlen($_POST['m']) > 9 )
    				$query .= " AND HOUR($wpdb->posts.post_date)=" . substr($_POST['m'], 8, 2);
    			if ( strlen($_POST['m']) > 11 )
    				$query .= " AND MINUTE($wpdb->posts.post_date)=" . substr($_POST['m'], 10, 2);
    			if ( strlen($_POST['m']) > 13 )
    				$query .= " AND SECOND($wpdb->posts.post_date)=" . substr($_POST['m'], 12, 2);
    		}
    
    		$query .= " ORDER BY post_date DESC";
    
    		$orders = $wpdb->get_results($query);
    
    		// Keep up to 12MB in memory, if becomes bigger write to temp file
    		$file = fopen('php://temp/maxmemory:'. (12*1024*1024), 'r+');
    
    		$csv_fields= array('order_id', 'status', 'received_date', 'paid_date', 'shipped_date', 'tax', 'shipping', 'total', 'coupon_discount', 'coupon_code', 'sku','name','quantity','price','email', 'name', 'address1', 'address2', 'city', 'state', 'zipcode', 'country', 'phone', 'shipping_method', 'shipping_method_option', 'special_instructions', 'gateway', 'gateway_method', 'payment_currency', 'transaction_id');
    
    		fputcsv( $file, $csv_fields );
    
    		//loop through orders and add rows
    		foreach ($orders as $order) {
    			$meta = get_post_custom($order->ID);
    
    			//unserialize a and add to object
    			foreach ($meta as $key => $val)
    				$order->$key = maybe_unserialize($meta[$key][0]);
    
    			//items
    			if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
    				foreach ($order->mp_cart_info as $product_id => $variations) {
    					foreach ($variations as $variation => $data) {
    
    						$fields = array();
    						$fields['order_id'] = $order->post_title;
    						$fields['status'] = $order->post_status;
    						$fields['received_date'] = $order->post_date;
    						$fields['paid_date'] = isset($order->mp_paid_time) ? date('Y-m-d H:i:s', $order->mp_paid_time) : null;
    						$fields['shipped_date'] = isset($order->mp_shipped_time) ? date('Y-m-d H:i:s', $order->mp_paid_time) : null;
    						$fields['tax'] = $order->mp_tax_total;
    						$fields['shipping'] = $order->mp_shipping_total;
    						$fields['total'] = $order->mp_order_total;
    						$fields['coupon_discount'] = @$order->mp_discount_info['discount'];
    						$fields['coupon_code'] = @$order->mp_discount_info['code'];
    
    						$price = $this->coupon_value_product($fields['coupon_code'], $data['price'] * $data['quantity'], $product_id);
    
    						$fields['sku'] = $data['SKU'];
    						$fields['name'] = $data['name'];
    						$fields['quantity'] = number_format_i18n($data['quantity']);
    						$fields['price'] = number_format_i18n($price / $data['quantity'], 2)." ".$order->mp_payment_info['currency'];
    						$fields['email'] = @$order->mp_shipping_info['email'];
    						$fields['name'] = @$order->mp_shipping_info['name'];
    						$fields['address1'] = @$order->mp_shipping_info['address1'];
    						$fields['address2'] = @$order->mp_shipping_info['address2'];
    						$fields['city'] = @$order->mp_shipping_info['city'];
    						$fields['state'] = @$order->mp_shipping_info['state'];
    						$fields['zipcode'] = @$order->mp_shipping_info['zip'];
    						$fields['country'] = @$order->mp_shipping_info['country'];
    						$fields['phone'] = @$order->mp_shipping_info['phone'];
    						$fields['shipping_method'] = @$order->mp_shipping_info['shipping_option'];
    						$fields['shipping_method_option'] = @$order->mp_shipping_info['shipping_sub_option'];
    						$fields['special_instructions'] = @$order->mp_shipping_info['special_instructions'];
    						$fields['gateway'] = @$order->mp_payment_info['gateway_private_name'];
    						$fields['gateway_method'] = @$order->mp_payment_info['method'];
    						$fields['payment_currency'] = @$order->mp_payment_info['currency'];
    						$fields['transaction_id'] = @$order->mp_payment_info['transaction_id'];
    
    					    fputcsv( $file, $fields );
    					}
    				}
    			} else {
    
    			}
    
    		}
    
    		//create our filename
    		$filename = 'orders_export';
    		$filename .= isset($_POST['m']) ? '_' . $_POST['m'] : '';
    		$filename .= '_' . time() . '.csv';
    
    		//serve the file
    		rewind($file);
    		ob_end_clean(); //kills any buffers set by other plugins
    		header('Content-Description: File Transfer');
    		header('Content-Type: text/csv');
    		header('Content-Disposition: attachment; filename="'.$filename.'"');
    		header('Content-Transfer-Encoding: binary');
    		header('Expires: 0');
    		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    		header('Pragma: public');
    		$output = stream_get_contents($file);
    		$output = "\xEF\xBB\xBF" . $output; // UTF-8 BOM
    		header('Content-Length: ' . strlen($output));
    		fclose($file);
    		die($output);
    	}

    Step 2 : Please call this newly created function in export_orders_csv function just after we checked permission for access.

    Before :

    //check permissions
    		$post_type_object = get_post_type_object('mp_order');
    		if ( !current_user_can($post_type_object->cap->edit_posts) )
    			wp_die(__('Cheatin’ uh?'));

    After :

    //check permissions
    		$post_type_object = get_post_type_object('mp_order');
    		if ( !current_user_can($post_type_object->cap->edit_posts) )
    			wp_die(__('Cheatin’ uh?'));
    
    		$display_item_per_row = true;
    
    		if(	$display_item_per_row == true )
    		{
    			$this->export_orders_csv_per_items();
    			die();
    		}

    Whenever you want to switch to previous version of csv, just set $display_item_per_row to false.

    I hope that helps. Please feel free to ask if you have more questions on the same.

    Best Regards
    Sandeep Kumar

  • Vinod Dalvi

    Hi Sandeep,

    Thank you for chiming in here and posting the ultimate solution.

    Hi Sherry,

    You are most welcome here :slight_smile:

    If you make these changes in the plugin file then updating the plugin will overwrite the changes and you will have to make these changes every time when you update the MarketPress plugin. You can make these changes update proof by adding the function export_orders_csv_per_items in the functions.php file of your child theme">child theme and making the following changes in it.

    Before making changes:

    global $wpdb;

    $price = $this->coupon_value_product($fields['coupon_code'], $data['price'] * $data['quantity'], $product_id);

    After making changes:

    global $wpdb, $mp;

    $price = $mp->coupon_value_product($fields['coupon_code'], $data['price'] * $data['quantity'], $product_id);

    Also add the following code in the functions.php file and don't make any changes in the MarketPress plugin files.

    global $mp;
    remove_action( 'wp_ajax_mp-orders-export', array( $mp, 'export_orders_csv' ) );
    add_action( 'wp_ajax_mp-orders-export', 'export_orders_csv_per_items' );

    Just comment / remove the above code if you want to use previous version of csv

    Best Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.