Skip to content

Instantly share code, notes, and snippets.

@MattRyanCo
Created September 27, 2017 19:51
Show Gist options
  • Save MattRyanCo/606c47eea7b7f8f8931948e76b402312 to your computer and use it in GitHub Desktop.
Save MattRyanCo/606c47eea7b7f8f8931948e76b402312 to your computer and use it in GitHub Desktop.
Add Size Attribute Column to WooCommerce Custom Exporter
// Ref: https://github.com/skyverge/wc-plugins-snippets/blob/master/woocommerce-customer-order-csv-export/add-product-attributes-to-one-row-per-item-order-export.php
/**
* Adds size attribute to the one row per item order export
* CANNOT be used with non-one row per item formats
*/
/**
* Add a 'size' column to the export file
*
* @param array $headers
* @return array
*/
/**
* Adds size attribute to the one row per item order export
* CANNOT be used with non-one row per item formats
*/
/**
* Add a 'size' column to the export file
*
* @param array $headers
* @return array
*/
function cws_sv_wc_csv_export_add_product_attributes_column( $headers ) {
$new_headers = array();
foreach ( $headers as $key => $header ) {
$new_headers[ $key ] = $header;
if ( 'item_name' === $key ) {
$new_headers['size'] = 'size';
}
}
return $new_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'cws_sv_wc_csv_export_add_product_attributes_column' );
/**
* Add the WC_Product object to the line item data for use by the one row per item
* filter below
*
* @param array $line_item
* @param array $_ item data, unused
* @param \WC_Product $product
* @return array
*/
function cws_sv_wc_csv_export_add_product_to_order_line_item( $line_item, $_, $product ) {
$line_item['product'] = $product;
return $line_item;
}
add_filter( 'wc_customer_order_csv_export_order_line_item', 'cws_sv_wc_csv_export_add_product_to_order_line_item', 10, 3 );
/**
* Add the product attributes in the format:
*
* attribute name=attribute value 1, attribute value 2, etc.
*
* @param array $order_data
* @param array $item
* @return array
*/
function cws_sv_wc_csv_export_add_product_attributes( $order_data, $item ) {
$order_data['size'] = '';
$count = 1;
if ( ! is_object( $item['product'] ) ) {
return $order_data;
}
foreach ( array_keys( $item['product']->get_attributes() ) as $attribute ) {
// Use next line for format: attribute value 1, attribute value 2, etc.
$order_data['size'] .= $item['product']->get_attribute( $attribute );
// uncomment this line for format: attribute name=attribute value 1, attribute value 2, etc.
// $order_data['product_attributes'] .= str_replace( 'pa_', '', $attribute ) . '=' . $item['product']->get_attribute( $attribute );
// add a semicolon divider if there are multiple attributes and it's not the last one
if ( count( $item['product']->get_attributes() ) > 1 && $count !== count( $item['product']->get_attributes() ) ) {
$order_data['size'] .= ';';
}
$count++;
}
return $order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row_one_row_per_item', 'cws_sv_wc_csv_export_add_product_attributes', 10, 2 );
function cws_sv_wc_csv_export_add_product_attributes_column( $headers ) {
$new_headers = array();
foreach ( $headers as $key => $header ) {
$new_headers[ $key ] = $header;
if ( 'item_name' === $key ) {
$new_headers['size'] = 'size';
}
}
return $new_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'cws_sv_wc_csv_export_add_product_attributes_column' );
/**
* Add the WC_Product object to the line item data for use by the one row per item
* filter below
*
* @param array $line_item
* @param array $_ item data, unused
* @param \WC_Product $product
* @return array
*/
function cws_sv_wc_csv_export_add_product_to_order_line_item( $line_item, $_, $product ) {
$line_item['product'] = $product;
return $line_item;
}
add_filter( 'wc_customer_order_csv_export_order_line_item', 'cws_sv_wc_csv_export_add_product_to_order_line_item', 10, 3 );
/**
* Add the product attributes in the format:
*
* attribute name=attribute value 1, attribute value 2, etc.
*
* @param array $order_data
* @param array $item
* @return array
*/
function cws_sv_wc_csv_export_add_product_attributes( $order_data, $item ) {
$order_data['size'] = '';
$count = 1;
if ( ! is_object( $item['product'] ) ) {
return $order_data;
}
foreach ( array_keys( $item['product']->get_attributes() ) as $attribute ) {
$order_data['size'] .= $item['product']->get_attribute( $attribute );
// $order_data['product_attributes'] .= str_replace( 'pa_', '', $attribute ) . '=' . $item['product']->get_attribute( $attribute );
// add a semicolon divider if there are multiple attributes and it's not the last one
if ( count( $item['product']->get_attributes() ) > 1 && $count !== count( $item['product']->get_attributes() ) ) {
$order_data['size'] .= ';';
}
$count++;
}
return $order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row_one_row_per_item', 'cws_sv_wc_csv_export_add_product_attributes', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment