Last active
January 9, 2019 04:17
-
-
Save wpmudev-sls/f0580cb0baee0a00a993cbb593fc14d0 to your computer and use it in GitHub Desktop.
[MarketPress] - Export Orders
This file contains hidden or 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
<?php | |
/** | |
* Plugin Name: [MarketPress] - Export Orders | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Ability to export MarketPress orders to CSV | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MP_Export_orders' ) ) { | |
class WPMUDEV_MP_Export_orders { | |
private static $_instance = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_MP_Export_orders(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts' ) ); | |
add_action( 'admin_footer',array( &$this, 'admin_js_scripts' ) ); | |
add_action( 'admin_menu', array( &$this, 'register_export_menu' ), 99 ); | |
add_action( 'load-store-settings_page_mp-export-orders', array( &$this, 'export_orders_action' ) ); | |
} | |
public function export_orders_action(){ | |
$data = $_POST; | |
if( ! isset( $data['action'] ) || 'mp-export-orders' != $data['action'] ){ | |
return; | |
} | |
unset( $data['action'] ); | |
self::export_orders( $data ); | |
} | |
public static function export_orders( $args ){ | |
global $wpdb; | |
//check permissions | |
if ( ! current_user_can( 'manage_options' ) ) | |
wp_die( __( 'Cheatin’ uh?', 'mp' ) ); | |
$defaults = array( | |
'start' => '', | |
'end' => '', | |
'order_status' => '' | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
$where_A_ = array(); | |
if( isset( $args['order_status'] ) && is_array( $args['order_status'] ) ){ | |
$status_in = array(); | |
foreach( $args['order_status'] as $status ){ | |
$status_in[] = "'{$status}'"; | |
} | |
$where_A_[] = 'post_status IN ( ' . implode( ',', $status_in ) . ' )' ; | |
} | |
if( isset( $args['start'] ) && '' != $args['start'] ){ | |
//$from = DateTime::CreateFromFormat( "Y-m-d", $args['start'] ); | |
$start = $args['start'] . ' 00:00:00'; | |
$where_A_[] = "post_date >='{$start}'"; | |
} | |
if( isset( $args['end'] ) && '' != $args['end'] ){ | |
$end = $args['end'] . ' 23:59:59'; | |
$where_A_[] = "post_date <='{$end}'"; | |
} | |
$where = ! empty( $where_A_ ) ? ' AND ' . implode( ' AND ', $where_A_ ) : ''; | |
$query = "SELECT ID, post_title, post_date, post_status FROM {$wpdb->posts} WHERE post_type='mp_order' {$where}"; | |
$orders = $wpdb->get_results($query); | |
$file = fopen('php://temp/maxmemory:'. (12*1024*1024), 'r+'); | |
fputcsv( $file, | |
array('order_id', 'status', 'received_date', 'paid_date', 'shipped_date', 'tax', 'shipping', 'total', 'coupon_discount', 'coupon_code', 'item_count', 'items', 'email', 'name', 'address1', 'address2', 'city', 'state', 'zipcode', 'country', 'phone', 'shipping_method', 'shipping_method_option', 'special_instructions', 'gateway', 'gateway_method', 'payment_currency', 'transaction_id' ) | |
); | |
foreach ($orders as $order) { | |
$meta = get_post_custom($order->ID); | |
foreach ($meta as $key => $val) { | |
$order->$key = maybe_unserialize($meta[$key][0]); | |
} | |
$coupon_codes_used = ''; | |
$coupons_total_discount = 0; | |
if( isset( $order->mp_discount_info ) && is_array( $order->mp_discount_info ) ){ | |
foreach ( $order->mp_discount_info as $coupon_code => $coupon_discount ){ | |
$coupon_codes_used .= "\r\n{$coupon_code}"; | |
$coupons_total_discount += $coupon_discount; | |
} | |
} | |
$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_shipped_time) : null; | |
$fields['tax'] = $order->mp_tax_total; | |
$fields['shipping'] = $order->mp_shipping_total; | |
$fields['total'] = $order->mp_order_total; | |
$fields['coupon_discount'] = $coupons_total_discount; | |
$fields['coupon_code'] = $coupon_codes_used; | |
$fields['item_count'] = $order->mp_order_items; | |
$fields['items'] = ''; | |
//items | |
if (is_array($order->mp_cart_items) && count($order->mp_cart_items)) { | |
foreach ($order->mp_cart_items as $product_id => $variations) { | |
foreach ($variations as $variation => $data) { | |
if (!empty($fields['items'])) | |
$fields['items'] .= "\r\n"; | |
if (!empty($data['SKU'])) | |
$fields['items'] .= '[' . $data['SKU'] . '] '; | |
$fields['items'] .= $data['name'] . ': ' . number_format_i18n($data['quantity']) . ' * ' . mp_number_format($data['price'], 2) . ' ' . $order->mp_payment_info['currency']; | |
} | |
} | |
} else { | |
$fields['items'] = 'N/A'; | |
} | |
$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 ); | |
} | |
//create our filename | |
$filename = 'orders_export'; | |
$filename .= isset($_POST['m']) ? '_' . $_POST['m'] : ''; | |
$filename .= '_' . time() . '.csv'; | |
//serve the file | |
rewind($file); | |
ob_end_clean(); | |
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); | |
} | |
public function register_export_menu() { | |
add_submenu_page( | |
'store-settings', | |
'Export Orders', | |
'Export Orders', | |
'manage_options', | |
'mp-export-orders', | |
array(&$this, 'export_orders_page') | |
); | |
} | |
public static function array_to_html( $array, $args ) { | |
$html = ''; | |
$defaults = array ( | |
'format' => 'ckeckbox', | |
'selected' => 'none', | |
'name' => 'checkbox_field' | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
switch ( $args['format'] ) { | |
case 'ckeckbox': | |
default: | |
foreach( $array as $array_key => $array_val ){ | |
$html .= '<div><label>'; | |
$html .= '<input type="checkbox" | |
name="' . $args['name'] .'[]" | |
value="' . $array_key . '" />'; | |
$html .= '<span>' . $array_val . '</span>'; | |
$html .= '</label></div>'; | |
} | |
break; | |
} | |
return $html; | |
} | |
public function export_orders_page() { | |
$available_statuses = $this->get_order_statuses(); | |
$statuses_html = self::array_to_html( | |
$available_statuses, | |
array( | |
'format' => 'checkbox', | |
'selected' => 'all', | |
'name' => 'order_status' | |
) | |
); | |
?> | |
<div class="wrap mp-wrap"> | |
<h2 class="mp-settings-title"><?php _e( 'Export Orders', 'mp' ); ?></h2> | |
<div class="clear"></div> | |
<div class="mp-settings"> | |
<form method="post"> | |
<div class="clear" style="clear:both; margin-top: 20px;"></div> | |
<div> | |
<big><?php _e( 'Orders made between', 'mp' ); ?></big> | |
</div> | |
<div class="clear" style="clear:both;"></div> | |
<div class="col-half" style="float: left; width: 45%;"> | |
<label> | |
<strong><?php _e( 'Start date', 'mp' ); ?></strong> | |
<input type="date" id="mp-export-from-date" name="start" value="" class="mp-datepicker" /> | |
</label> | |
</div> | |
<div class="col-half" style="float: left; width: 45%;"> | |
<label> | |
<strong><?php _e( 'End date', 'mp' ); ?></strong> | |
<input type="date" id="mp-export-until-date" name="end" value="" class="mp-datepicker" /> | |
</label> | |
</div> | |
<div class="clear" style="clear:both; height: 20px;"></div> | |
<div> | |
<label> | |
<strong><?php _e( 'Orders that have status: ', 'mp' ) ?></strong> | |
<?php echo $statuses_html; ?> | |
</label> | |
</div> | |
<div class="clear" style="clear:both; margin-top: 20px;"></div> | |
<div> | |
<input type="submit" value="<?php _e( 'Export Orders', 'mp' ); ?>" /> | |
<input type="hidden" name="action" value="mp-export-orders" /> | |
</div> | |
</form> | |
</div> | |
</div> | |
<?php | |
} | |
public function get_order_statuses(){ | |
$order_statuses = get_post_stati( array('post_type' => 'mp_order'), 'objects' ); | |
$out = array(); | |
foreach( $order_statuses as $key => $order_status ){ | |
$out[ $key ] = $order_status->label; | |
} | |
return $out; | |
} | |
public function admin_js_scripts(){ | |
$screen = get_current_screen(); | |
if( 'store-settings_page_mp-export-orders' != $screen->id ) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(function($,d){ | |
$(d).ready(function(){ | |
$(document).ready(function(){ | |
$('.mp-datepicker').datepicker({ dateFormat: 'yy-mm-dd' }); | |
}); | |
}); | |
})(jQuery,document); | |
</script> | |
<?php | |
} | |
public function admin_enqueue_scripts(){ | |
$screen = get_current_screen(); | |
if( 'store-settings_page_mp-export-orders' != $screen->id ) { | |
return; | |
} | |
wp_enqueue_script('jquery-ui-datepicker'); | |
wp_register_style('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css'); | |
wp_enqueue_style('jquery-ui'); | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_MP_Export_orders'] = WPMUDEV_MP_Export_orders::get_instance(); | |
}, 10 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment