Skip to content

Commit

Permalink
Merge pull request #12 from vbiweb/dev
Browse files Browse the repository at this point in the history
New Feature - Bulk Export to CSV
  • Loading branch information
kgkrishnavbi authored Nov 6, 2019
2 parents 0365397 + b6cbac9 commit a9eb49d
Show file tree
Hide file tree
Showing 12 changed files with 298 additions and 38 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ Donate link: https://kgopalkrishna.com <br>
Tags: woocommerce, order, view <br>
Requires at least: 5.0.0 <br>
Tested up to: 5.2.3 <br>
Stable tag: 1.2.1 <br>
Stable tag: 1.3.0 <br>
License: GPLv3 or later <br>
License URI: http://www.gnu.org/licenses/gpl-3.0.html <br>

This plugin gives a strict View-Only access to Woocommerce orders for specified user roles.

== Description ==

This plugin gives a strict view only access to woocommerce orders. All you need to do is create a new user role 'products_admin' using any role editor plugin available. All users with this role will be able to view orders, the items in the order, total amount paid including taxes and shipping, and the order notes all in read-only mode. This plugin also provides support for select 3rd party Woocommerce premium plugins like Woocommerce Subscriptions, PDF Invoices and Woocommerce API Manager. Planned features include CSV Exporting options and Custom Order actions available only for 'products_admin'
This plugin gives a strict view only access to woocommerce orders. All you need to do is create a new user role 'products_admin' using any role editor plugin available. All users with this role will be able to view orders, the items in the order, total amount paid including taxes and shipping, and the order notes all in read-only mode. This plugin also provides support for select 3rd party Woocommerce premium plugins like Woocommerce Subscriptions, PDF Invoices and Woocommerce API Manager. You can also export orders in CSV format. Planned features include Custom Order actions available only for 'products_admin', settings for orders display page, etc.

== Installation ==

Expand All @@ -31,7 +31,7 @@ This plugin gives a strict view only access to woocommerce orders. All you need

= 1.1.0 =
* Fix - Fixed cancelled orders not visible #5
* Feature - Support for 3rd party woocommerce plugins ( PDF Invoices, Woocommerce Subscriptions, Woocommerce API Mananger )
* Feature - Support for 3rd party woocommerce plugins ( PDF Invoice, Woocommerce Subscriptions, Woocommerce API Mananger )
* Dev - New hooks added to the view order details page ( Details will be shared in a separate documentation )

= 1.2.0 =
Expand All @@ -42,17 +42,24 @@ This plugin gives a strict view only access to woocommerce orders. All you need
= 1.2.1 =
* Fix - Added function call to updated in the main plugin file #10

= 1.3.0 =
* Feature - Bulk Export to CSV

== Features ==

1. Strict view only access to orders.
1. View each orders individually.
1. Support for 3rd party woocommerce plugins ( PDF Invoices, Woocommerce Subscriptions, Woocommerce API Mananger )
1. Automatic update notifications
1. Export order details in CSV format

== Planned Features ==

* Export orders in CSV format
* Custom order view actions
* Role Editor for order view
* Settings to toggle display of order details metaboxes
* Settings to add custom postmeta fields to export columns
* About Plugin page

Here's a link to [WordPress](http://wordpress.org/ "Your favorite software") and one to [Markdown's Syntax Documentation][markdown syntax].
Titles are optional, naturally.
Expand Down
17 changes: 11 additions & 6 deletions admin/class-wc-order-view-admin-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,16 @@ public function no_items() {
* @since 1.0.0
*/
function get_columns() {

$active_plugins = get_option( 'active_plugins' );

$columns = array(
'cb' => __( '<input type="checkbox" />', 'wc-order-view' ),
'order_id' => __( 'Order', 'wc-order-view' ),
'customer_name' => __( 'Name', 'wc-order-view' )
);

if( get_option( 'wcov_pdf_invoices' ) == "enabled" ) {
if( get_option( 'wcov_pdf_invoices' ) == "enabled" && in_array( "woocommerce-pdf-invoice/woocommerce-pdf-invoice.php" , $active_plugins ) ) {
$columns[ 'pdf_invoice' ] = __( 'Invoice', 'wc-order-view' );
}

Expand All @@ -224,8 +226,7 @@ function get_columns() {
$columns[ 'billing_address' ] = __( 'Billing', 'wc-order-view' );
$columns[ 'shipping_address' ] = __( 'Ship To', 'wc-order-view' );

if( get_option( 'wcov_subscriptions' ) == "enabled" ) {

if( get_option( 'wcov_subscriptions' ) == "enabled" && in_array( "woocommerce-subscriptions/woocommerce-subscriptions.php" , $active_plugins ) ) {
$columns[ 'subscription_relationship' ] = '<div class="subscription_head tooltip"><span class="tooltiptext">' . esc_attr__( 'Subscription Relationship', 'wc-order-view' ) . '</span></div>';
}

Expand Down Expand Up @@ -458,7 +459,7 @@ function column_subscription_relationship( $item ) {
public function get_bulk_actions() {

$actions = array(
'bulk-export' => 'Export to CSV'
'wcov-export' => 'Export to CSV'
);

return $actions;
Expand Down Expand Up @@ -504,7 +505,7 @@ public function prepare_items() {
$this->_column_headers = array($columns, $hidden, $sortable);

/** Process bulk action */
//$this->process_bulk_action();
$this->process_bulk_action();

$per_page = $this->get_items_per_page( 'orders_per_page', 20 );
$current_page = $this->get_pagenum();
Expand All @@ -523,7 +524,11 @@ public function prepare_items() {

}


/**
* Allows to add filter fields at the top of the table.
*
* @since 1.0.0
*/
public function extra_tablenav( $which ) {

global $wpdb;
Expand Down
233 changes: 233 additions & 0 deletions admin/class-wc-order-view-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,237 @@ public function wc_order_view_settings_page() {

}


/**
* Method to export all orders in CSV Format
*
* @since 1.3.0
*/
public function bulk_export_to_csv() {

if( ( isset( $_GET[ 'action' ] ) && $_GET[ 'action' ] == "wcov-export" ) || ( isset( $_GET[ 'action2' ] ) && $_GET[ 'action2' ] == "wcov-export" ) ) {

$export_ids = esc_sql( $_GET['bulk-export'] );

$orders = array();

$active_plugins = get_option( 'active_plugins' );

if( empty( $export_ids ) ) {

$meta_query = array();
$meta_query['relation'] = "AND";

if ( isset ( $_REQUEST[ '_customer_user' ] ) && $_REQUEST[ '_customer_user' ] != "" ) {
$meta_query[] = array ( 'key' => '_customer_user', value => $_REQUEST[ '_customer_user' ] );
}

if ( isset ( $_REQUEST[ '_payment_method' ] ) && $_REQUEST[ '_payment_method' ] != "" ) {
$meta_query[] = array ( 'key' => '_payment_method', value => $_REQUEST[ '_payment_method' ] );
}

if ( isset ( $_REQUEST[ 's' ] ) && $_REQUEST[ 's' ] != "" ) {
$search_meta = array();
$search_meta[ 'relation' ] = "OR";

$search_meta[] = array ( 'key' => '_billing_first_name', value => $_REQUEST[ 's' ], 'compare' => 'LIKE' );
$search_meta[] = array ( 'key' => '_billing_last_name', value => $_REQUEST[ 's' ], 'compare' => 'LIKE' );
$search_meta[] = array ( 'key' => '_billing_email', value => $_REQUEST[ 's' ], 'compare' => 'LIKE' );
$search_meta[] = array ( 'key' => '_billing_company', value => $_REQUEST[ 's' ], 'compare' => 'LIKE' );
$search_meta[] = array ( 'key' => '_invoice_number_display', value => $_REQUEST[ 's' ], 'compare' => 'LIKE' );

$meta_query[] = $search_meta;
}

$args = array (
'numberposts' => -1,
'post_type' => 'shop_order',
'post_status' => ( isset ( $_REQUEST[ 'post_status' ] ) && $_REQUEST[ 'post_status' ] != "" ) ? $_REQUEST[ 'post_status' ] : array_keys( wc_get_order_statuses() ),
'meta_query' => $meta_query
);

if( isset ( $_REQUEST[ 'author' ] ) && $_REQUEST[ 'author' ] != "" ) {
$args[ 'author' ] = $_REQUEST[ 'author' ];
}

if ( isset ( $_REQUEST[ 'm' ] ) && $_REQUEST[ 'm' ] != "" ) {
$year = date( "Y", strtotime ( $_REQUEST[ 'm' ] . "01" ) );
$month = date( "m", strtotime ( $_REQUEST[ 'm' ] . "01" ) );

$date_query = array(
array(
'year' => $year,
'month' => $month
),
);

$args[ 'date_query' ] = $date_query;

}

$result = get_posts ( $args );

foreach ( $result as $result_item ) {

$order_item = array();

$order = new WC_Order( $result_item->ID );
$order_items = $order->get_items( 'line_item' );

$billing_address = apply_filters( 'woocommerce_order_formatted_billing_address', $order->get_address( 'billing' ), $order );
$billing_address = WC()->countries->get_formatted_address( $billing_address, ', ' );

$shipping_address = apply_filters( 'woocommerce_order_formatted_shipping_address', $order->get_address( 'shipping' ), $order );
$shipping_address = WC()->countries->get_formatted_address( $shipping_address, ', ' );

$products = array();

foreach ( $order_items as $item ) {
$products[] = $item->get_quantity() . ' x [' . $item->get_name() . ']';
}

$order_item[ 'Order ID' ] = $order->get_id();
$order_item[ 'Date' ] = date( "d-m-Y", strtotime( $order->get_date_completed() ) );
$order_item[ 'Order Status' ] = ucfirst( $order->get_status() );
$order_item[ 'First Name' ] = $order->get_billing_first_name();
$order_item[ 'Last Name' ] = $order->get_billing_last_name();
$order_item[ 'Email' ] = $order->get_billing_email();
$order_item[ 'Phone' ] = $order->get_billing_phone();
$order_item[ 'Company' ] = $order->get_billing_company();
$order_item[ 'Billing Address' ] = $billing_address ? $billing_address : "";
$order_item[ 'Shipping Address' ] = $shipping_address ? $shipping_address : "";
$order_item[ 'Products' ] = implode("; ", $products);
$order_item[ 'Payment Method' ] = $order->get_payment_method_title();
$order_item[ 'Total' ] = $order->get_total();

if( get_option( 'wcov_subscriptions' ) == "enabled" && in_array( "woocommerce-subscriptions/woocommerce-subscriptions.php" , $active_plugins ) ) {

$subscription_relationship = "";

if ( wcs_order_contains_subscription( $order->get_id(), 'renewal' ) ) {
$subscription_relationship = "Renewal Order";
} elseif ( wcs_order_contains_subscription( $order->get_id(), 'resubscribe' ) ) {
$subscription_relationship = "Resubscribtion Order";
} elseif ( wcs_order_contains_subscription( $order->get_id(), 'parent' ) ) {
$subscription_relationship = "Parent Order";
}

$order_item[ 'Subscription Relationship' ] = $subscription_relationship;

}

if( get_option( 'wcov_pdf_invoices' ) == "enabled" && in_array( "woocommerce-pdf-invoice/woocommerce-pdf-invoice.php" , $active_plugins ) ) {

$order_item[ 'Invoice Number' ] = get_post_meta( $order->get_id(), '_invoice_number_display', true );

}

$orders[] = $order_item;

}

} else {

foreach ( $export_ids as $export_id ) {

$order_item = array();

$order = new WC_Order( $export_id );
$order_items = $order->get_items( 'line_item' );

$billing_address = apply_filters( 'woocommerce_order_formatted_billing_address', $order->get_address( 'billing' ), $order );
$billing_address = WC()->countries->get_formatted_address( $billing_address, ', ' );

$shipping_address = apply_filters( 'woocommerce_order_formatted_shipping_address', $order->get_address( 'shipping' ), $order );
$shipping_address = WC()->countries->get_formatted_address( $shipping_address, ', ' );

$products = array();

foreach ( $order_items as $item ) {
$products[] = $item->get_quantity() . ' x [' . $item->get_name() . ']';
}

$order_item[ 'Order ID' ] = $order->get_id();
$order_item[ 'Date' ] = date( "d-m-Y", strtotime( $order->get_date_completed() ) );
$order_item[ 'Order Status' ] = ucfirst( $order->get_status() );
$order_item[ 'First Name' ] = $order->get_billing_first_name();
$order_item[ 'Last Name' ] = $order->get_billing_last_name();
$order_item[ 'Email' ] = $order->get_billing_email();
$order_item[ 'Phone' ] = $order->get_billing_phone();
$order_item[ 'Company' ] = $order->get_billing_company();
$order_item[ 'Billing Address' ] = $billing_address ? $billing_address : "";
$order_item[ 'Shipping Address' ] = $shipping_address ? $shipping_address : "";
$order_item[ 'Products' ] = implode("; ", $products);
$order_item[ 'Payment Method' ] = $order->get_payment_method_title();
$order_item[ 'Total' ] = $order->get_total();

if( get_option( 'wcov_subscriptions' ) == "enabled" && in_array( "woocommerce-subscriptions/woocommerce-subscriptions.php" , $active_plugins ) ) {

$subscription_relationship = "";

if ( wcs_order_contains_subscription( $order->get_id(), 'renewal' ) ) {
$subscription_relationship = "Renewal Order";
} elseif ( wcs_order_contains_subscription( $order->get_id(), 'resubscribe' ) ) {
$subscription_relationship = "Resubscribtion Order";
} elseif ( wcs_order_contains_subscription( $order->get_id(), 'parent' ) ) {
$subscription_relationship = "Parent Order";
}

$order_item[ 'Subscription Relationship' ] = $subscription_relationship;

}

if( get_option( 'wcov_pdf_invoices' ) == "enabled" && in_array( "woocommerce-pdf-invoice/woocommerce-pdf-invoice.php" , $active_plugins ) ) {

$order_item[ 'Invoice Number' ] = get_post_meta( $order->get_id(), '_invoice_number_display', true );

}

$orders[] = $order_item;

}

}

$date = date("d-m-Y");

$header = array( 'Order ID', 'Date', 'Order Status', 'First Name', 'Last Name', 'Email', 'Phone', 'Company', 'Billing Address', 'Shipping Address', 'Products', 'Payment Method', 'Total' );

if( get_option( 'wcov_subscriptions' && in_array( "woocommerce-subscriptions/woocommerce-subscriptions.php" , $active_plugins ) ) == "enabled" ) {
$header[] = "Subscription Relationship";
}

if( get_option( 'wcov_pdf_invoices' ) == "enabled" && in_array( "woocommerce-pdf-invoice/woocommerce-pdf-invoice.php" , $active_plugins ) ) {
$header[] = "Invoice Number";
}

header("Content-Type: application/csv");
header("Content-Disposition: attachment;Filename=wc-order-view-export-$date.csv");

$export_file = fopen('php://output', 'w+');

fputcsv( $export_file, $header );

foreach ( $orders as $order ) {

$record = array();

foreach ( $header as $field ) {

$record[] = $order[ $field ];

}

fputcsv( $export_file, $record );

}

fclose($export_file);

die();

}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

$is_subscription_screen = wcs_is_subscription( $post->ID );

if( get_option( 'wcov_api_manager' ) == 'enabled' && ! $is_subscription_screen ) { ?>
if( get_option( 'wcov_api_manager' ) == 'enabled' && ! $is_subscription_screen && in_array( "woocommerce-api-manager/woocommerce-api-manager.php" , $active_plugins ) ) { ?>

<div id="wc_am_api_key_activations" class="postbox ">
<button type="button" class="handlediv" aria-expanded="true"><span class="screen-reader-text">Toggle panel: API Key Activations</span><span class="toggle-indicator" aria-hidden="true"></span></button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

$is_subscription_screen = wcs_is_subscription( $post->ID );

if( get_option( 'wcov_api_manager' ) == 'enabled' && ! $is_subscription_screen ) { ?>
if( get_option( 'wcov_api_manager' ) == 'enabled' && ! $is_subscription_screen && in_array( "woocommerce-api-manager/woocommerce-api-manager.php" , $active_plugins ) ) { ?>

<div id="wc_am_api_keys" class="postbox ">
<button type="button" class="handlediv" aria-expanded="true"><span class="screen-reader-text">Toggle panel: API Key Products</span><span class="toggle-indicator" aria-hidden="true"></span></button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

$is_subscription_screen = wcs_is_subscription( $post->ID );

if( get_option( 'wcov_pdf_invoices' ) == "enabled" && ! $is_subscription_screen ) : ?>
if( get_option( 'wcov_pdf_invoices' ) == "enabled" && ! $is_subscription_screen && in_array( "woocommerce-pdf-invoice/woocommerce-pdf-invoice.php" , $active_plugins ) ) : ?>
<div id="woocommerce-invoice_details" class="postbox ">
<button type="button" class="handlediv" aria-expanded="true"><span class="screen-reader-text">Toggle panel: Invoice Details</span><span class="toggle-indicator" aria-hidden="true"></span></button>
<h2 class="hndle ui-sortable-handle"><span>Invoice Details</span></h2>
Expand Down
Loading

0 comments on commit a9eb49d

Please sign in to comment.