Last active
October 4, 2016 11:20
-
-
Save calliaweb/153969e651700f9ba29ecc1a95c2649c to your computer and use it in GitHub Desktop.
Export Woo & WC Vendors orders including commission rate and amount
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: Cambridge Food Collective Orders Export | |
Plugin URI: http://www.calliaweb.co.uk | |
Description: Adds Cambridge Food Collective orders export functionality | |
Version: 1.0.0 | |
Author: Jo Waltham | |
Author URI: http://www.calliaweb.co.uk | |
*/ | |
// if this file is called directly about | |
if ( ! defined('WPINC' )) { | |
die; | |
} | |
// create top level admin menu | |
add_action('admin_menu', 'cfc_orders_export_admin_menu'); | |
function cfc_orders_export_admin_menu() { | |
add_menu_page('CFC Orders Export', 'CFC Orders Export', 10, 'cfc_orders_export', 'cfc_orders_export_admin_page'); | |
} | |
function cfc_orders_export_admin_page() { | |
?> | |
<div class="wrap"> | |
<h2>Cambridge Food Collective Orders Export to CSV</h2> | |
<p>Click export to generate a csv file of all orders with status of processing or on hold.</p> | |
<form method="post" id="export-form" action=""> | |
<?php submit_button('Export', 'primary', 'download_csv' ); ?> | |
</form> | |
</div> | |
<?php | |
} | |
add_action('admin_init', 'cfc_orders_export_admin_init'); | |
function cfc_orders_export_admin_init() { | |
global $plugin_page; | |
if ( isset($_POST['download_csv']) && $plugin_page == 'cfc_orders_export' ) { | |
cfc_generate_orders_export(); | |
die(); | |
} | |
} | |
function cfc_generate_orders_export() { | |
// output headers so that the file is downloaded rather than displayed | |
header('Content-Type: text/csv; charset=utf-8'); | |
// set file name with current date | |
header('Content-Disposition: attachment; filename=cfc-orders-export-' . date('Y-m-d') . '.csv'); | |
// create a file pointer connected to the output stream | |
$output = fopen('php://output', 'w'); | |
// set the column headers for the csv | |
$headings = array( 'Order Number', 'Order Date', 'Customer', 'Producer', 'Product', 'Variation', 'Qty', 'Sub Total', 'Commission', 'Commission Type' ); | |
fputcsv( $output, $headings ); | |
$args = array( | |
'post_type' => 'shop_order', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'tax_query' => array( | |
array( | |
'taxonomy' => 'shop_order_status', | |
'field' => 'slug', | |
'terms' => array( 'processing', 'on-hold' ) | |
) | |
) | |
); | |
$loop = new WP_Query( $args ); | |
$vendor_product_qty = array(); | |
//loop through orders | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
$order = new WC_Order($loop->post->ID); | |
$customer = get_userdata( $order->user_id ); | |
$customer_name = $customer->first_name . ' ' . $customer->last_name; | |
if( empty( $customer->first_name ) && empty( $customer->last_name ) ) { | |
$customer_name = $customer->user_login; | |
} | |
// loop through each item in this order | |
foreach ($order->get_items() as $item) { | |
//$commission = WCV_Commission::get_commission_rate( $item['product_id'] ); | |
$commission = WCV_Commission::calculate_commission( $item[ 'line_subtotal' ], $item[ 'product_id' ], $order, $item[ 'qty' ] ); | |
$product_commission_type = get_post_meta( $item[ 'product_id' ], 'wcv_commission_type', true ); | |
// calculate_commission is not currently working for vendor commissions - this is a check for that - rate appears to be held in pv_custom_commission_rate | |
if( empty( $product_commission_type ) ) { | |
$vendor_id = get_post_field( 'post_author', $item[ 'product_id' ] ); | |
$commission_percentage = get_user_meta( $vendor_id, 'pv_custom_commission_rate', true ); | |
if( $commission_percentage ) { | |
$product_commission_type = 'producer percentage'; | |
$commission = round( $item['line_subtotal'] * $commission_percentage / 100, 2); | |
} | |
else { | |
$product_commission_type = 'default percentage'; | |
} | |
} | |
// Variation Name | |
$variation_title = ''; | |
if( 0 <> $item[ 'variation_id' ] ) { | |
$variation = wc_get_product( $item[ 'variation_id' ] ); | |
$variation_title = $variation->get_formatted_variation_attributes( true ); | |
} | |
// Output to csv | |
$row = array( $loop->post->ID, $order->order_date, $customer_name, $item['Producer'], $item['name'], $variation_title, $item['qty'], $item['line_subtotal'], $commission, $product_commission_type ); | |
fputcsv($output, $row); | |
} | |
endwhile; | |
wp_reset_postdata(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment