Skip to content

Instantly share code, notes, and snippets.

@roykho
Last active September 10, 2020 17:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roykho/b5e6849adacd9970fc6f to your computer and use it in GitHub Desktop.
Save roykho/b5e6849adacd9970fc6f to your computer and use it in GitHub Desktop.
WooCommerce Product Vendors 2.0 - Add columns to WooCommerce Customer and Orders CSV Export
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;
}
@alby111
Copy link

alby111 commented Feb 18, 2018

Hi Roykho I tried this code , but the Vendor column is empty and I can't see which order belongs to which vendor , thanks for your support

@agarwalc
Copy link

Hi roykho, thanks for this! It worked until 4.3 for us but not anymore with the latest 4.4.6 branch. Any hints on what we can update in this snippet to continue exporting Vendor names? Thanks a lot

@negru13
Copy link

negru13 commented Apr 1, 2020

Hi, after hitting the same wall, I have read line 15. In order to display the vendor's name in that column, you have to set the export to "One row per item" . Hope it helps someone.

@huckel78
Copy link

Sorry, I'm new in this. In which file I need to add that code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment