Created
March 20, 2020 23:16
-
-
Save NickGreen/d08a85bcd6040c7561b0eeed8aa70401 to your computer and use it in GitHub Desktop.
Add Delivery Date to Order Export CSV
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: Add delivery date to export | |
* Plugin URI: | |
* Description: | |
* Version: 1.0 | |
* Author: | |
* Author URI: | |
**/ | |
/** | |
* @param array $column_headers the original column headers | |
* @param \CSV_Export_Generator $csv_generator the generator instance | |
* @return array the updated column headers | |
*/ | |
function sv_wc_csv_export_modify_column_headers_example( $column_headers, $csv_generator ) { | |
// add the new `delivery date` column header | |
$column_headers['delivery_date'] = 'delivery date'; | |
return $column_headers; | |
} | |
add_filter( 'wc_customer_order_export_csv_order_headers', 'sv_wc_csv_export_modify_column_headers_example', 10, 2 ); | |
/** | |
* | |
* @param array $order_data the original column data | |
* @param \WC_Order $order the order object | |
* @param \CSV_Export_Generator $csv_generator the generator instance | |
* @return array the updated column data | |
*/ | |
function sv_wc_csv_export_modify_row_data_example( $order_data, $order, $csv_generator ) { | |
// Example showing how to extract order metadata into it's own column | |
$delivery_meta = is_callable( array( $order, 'get_meta' ) ) ? $order->get_meta( '_delivery_date' ) : $order->_delivery_date; | |
$custom_data = array( | |
'delivery_date' => $delivery_meta, | |
); | |
return sv_wc_csv_export_add_custom_order_data( $order_data, $custom_data, $csv_generator ); | |
} | |
add_filter( 'wc_customer_order_export_csv_order_row', 'sv_wc_csv_export_modify_row_data_example', 10, 3 ); | |
if ( ! function_exists( 'sv_wc_csv_export_add_custom_order_data' ) ) : | |
/** | |
* Helper function to add custom order data to CSV Export order data | |
* | |
* @param array $order_data the original column data that may be in One Row per Item format | |
* @param array $custom_data the custom column data being merged into the column data | |
* @param \CSV_Export_Generator $csv_generator the generator instance | |
* @return array the updated column data | |
*/ | |
function sv_wc_csv_export_add_custom_order_data( $order_data, $custom_data, $csv_generator ) { | |
$new_order_data = array(); | |
if ( sv_wc_csv_export_is_one_row( $csv_generator ) ) { | |
foreach ( $order_data as $data ) { | |
$new_order_data[] = array_merge( (array) $data, $custom_data ); | |
} | |
} else { | |
$new_order_data = array_merge( $order_data, $custom_data ); | |
} | |
return $new_order_data; | |
} | |
endif; | |
if ( ! function_exists( 'sv_wc_csv_export_is_one_row' ) ) : | |
/** | |
* Helper function to check the export format | |
* | |
* @param \CSV_Export_Generator $csv_generator the generator instance | |
* @return bool - true if this is a one row per item format | |
*/ | |
function sv_wc_csv_export_is_one_row( $csv_generator ) { | |
$one_row_per_item = false; | |
if ( version_compare( wc_customer_order_csv_export()->get_version(), '4.0.0', '<' ) ) { | |
// pre 4.0 compatibility | |
$one_row_per_item = ( 'default_one_row_per_item' === $csv_generator->order_format || 'legacy_one_row_per_item' === $csv_generator->order_format ); | |
} elseif ( isset( $csv_generator->format_definition ) ) { | |
// post 4.0 (requires 4.0.3+) | |
$one_row_per_item = 'item' === $csv_generator->format_definition['row_type']; | |
} | |
return $one_row_per_item; | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment