Last active
February 18, 2018 14:03
-
-
Save tamarazuk/7684698caa4a40e19c7d86aea2190aec to your computer and use it in GitHub Desktop.
WooCommerce Product Vendors 2.0 - Add columns to WooCommerce Customer/Order CSV Export
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 // only copy this line if needed | |
add_filter( 'wc_customer_order_csv_export_order_headers', 'wc_product_vendors_csv_export_integration_add_column_headers' ); | |
add_filter( 'wc_customer_order_csv_export_order_line_item', 'wc_product_vendors_csv_export_integration_modify_line_item', 10, 5 ); | |
add_filter( 'wc_customer_order_csv_export_order_row_one_row_per_item', 'wc_product_vendors_csv_export_integration_add_row_data', 10, 4 ); | |
function wc_product_vendors_csv_export_integration_add_column_headers( $headers ) { | |
$headers['vendor'] = 'Vendor'; | |
return $headers; | |
} | |
function wc_product_vendors_csv_export_integration_modify_line_item( $line_item, $item, $product, $order, $csv_generator ) { | |
// bail out as we only need this data if it's one row per item | |
if ( ! wc_product_vendors_csv_export_integration_is_one_row_per_item( $csv_generator ) ) { | |
return $order_data; | |
} | |
if ( $product instanceof WC_Product && 'product' === get_post_type( $product->id ) ) { | |
// add product id to line_item | |
$line_item['product_vendors_product_id'] = $product->id; | |
} | |
return $line_item; | |
} | |
function wc_product_vendors_csv_export_integration_add_row_data( $order_data, $item, $order, $csv_generator ) { | |
// bail out as this only works with one row per item | |
if ( ! wc_product_vendors_csv_export_integration_is_one_row_per_item( $csv_generator ) ) { | |
return $order_data; | |
} | |
if ( ! empty( $item['product_vendors_product_id'] ) ) { | |
$vendor = WC_Product_Vendors_Utils::is_vendor_product( $item['product_vendors_product_id'] ); | |
if ( ! empty( $vendor[0] ) ) { | |
$vendor_data = WC_Product_Vendors_Utils::get_vendor_data_by_id( $vendor[0]->term_id ); | |
if ( ! empty( $vendor_data ) ) { | |
$order_data['vendor'] = $vendor_data['name']; | |
} | |
} | |
} | |
return $order_data; | |
} | |
function wc_product_vendors_csv_export_integration_is_one_row_per_item( $csv_generator ) { | |
// sanity check - bail if CSV Export is not active, or if the provided paramater is not as expected | |
if ( ! function_exists( 'wc_customer_order_csv_export' ) || ! $csv_generator instanceof WC_Customer_Order_CSV_Export_Generator ) { | |
return false; | |
} | |
$one_row_per_item = false; | |
// determine if the selected format is "one row per item" | |
if ( version_compare( wc_customer_order_csv_export()->get_version(), '4.0.0', '<' ) ) { | |
$one_row_per_item = ( 'default_one_row_per_item' === $csv_generator->order_format || 'legacy_one_row_per_item' === $csv_generator->order_format ); | |
// v4.0.0 - 4.0.2 | |
} elseif ( ! isset( $csv_generator->format_definition ) ) { | |
// get the CSV Export format definition | |
$format_definition = wc_customer_order_csv_export()->get_formats_instance()->get_format( $csv_generator->export_type, $csv_generator->export_format ); | |
$one_row_per_item = isset( $format_definition['row_type'] ) && 'item' === $format_definition['row_type']; | |
// v4.0.3+ | |
} else { | |
$one_row_per_item = 'item' === $csv_generator->format_definition['row_type']; | |
} | |
return $one_row_per_item; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Tamara I tried your code , but the Vendor column is empty and I can't see which order belongs to which vendor , thanks for your support,