WooCommerce Customer/Order CSV 导出 - 添加列作为第一列
WooCommerce Customer/Order CSV Export - Add Column as first column
我正在使用 WooCommerce Customer/Order CSV 导出并在我的函数文件中有一个片段(从 WooCommerce 获得。
function sv_wc_csv_export_reorder_columns( $column_headers ) {
// remove order total from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['column_1'] );
unset( $column_headers['delivery_date'] );
$new_column_headers = array();
foreach ( $column_headers as $column_key => $column_name ) {
$new_column_headers[ $column_key ] = $column_name;
if ( 'shipping_company' == $column_key ) {
// add order total immediately after order_number
$new_column_headers['column_1'] = 'Contact Name';
}
}
return $new_column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns' );
问题与代码一样,它在 shipping_company(第一个字段)之后直接添加 column_1 - 我如何设置它以使其出现在该列之前或将其设置为第一列?
试试这个代码
function array_insert_after( array $array, $key, array $new ) {
$keys = array_keys( $array );
$index = array_search( $key, $keys );
$pos = false === $index ? count( $array ) : $index + 1;
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
}
function sv_wc_csv_export_reorder_columns( $column_headers ) {
// remove order total from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['column_1'] );
unset( $column_headers['delivery_date'] );
$new_column_headers = array();
$new_column_headers['column_1'] = 'Contact Name';
$column_headers = array_insert_after($column_headers, 'shipping_company', $new_column_headers);
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns' );
您只需执行以下操作即可将 'column_1' 设置或添加为第一列:
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns', 10, 2 );
function sv_wc_csv_export_reorder_columns( $column_headers, $csv_generator ) {
// Save "column_1" in a new array
$column1 = array('column_1' => __("Contact Name") );
// Remove "column_1" to be reordered
unset( $column_headers['column_1'] );
// Remove unnecessary "delivery_date"
unset( $column_headers['delivery_date'] );
// Set "column_1" as first column merging arrays
return $column1 + $column_headers;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。
我正在使用 WooCommerce Customer/Order CSV 导出并在我的函数文件中有一个片段(从 WooCommerce 获得。
function sv_wc_csv_export_reorder_columns( $column_headers ) {
// remove order total from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['column_1'] );
unset( $column_headers['delivery_date'] );
$new_column_headers = array();
foreach ( $column_headers as $column_key => $column_name ) {
$new_column_headers[ $column_key ] = $column_name;
if ( 'shipping_company' == $column_key ) {
// add order total immediately after order_number
$new_column_headers['column_1'] = 'Contact Name';
}
}
return $new_column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns' );
问题与代码一样,它在 shipping_company(第一个字段)之后直接添加 column_1 - 我如何设置它以使其出现在该列之前或将其设置为第一列?
试试这个代码
function array_insert_after( array $array, $key, array $new ) {
$keys = array_keys( $array );
$index = array_search( $key, $keys );
$pos = false === $index ? count( $array ) : $index + 1;
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
}
function sv_wc_csv_export_reorder_columns( $column_headers ) {
// remove order total from the original set of column headers, otherwise it will be duplicated
unset( $column_headers['column_1'] );
unset( $column_headers['delivery_date'] );
$new_column_headers = array();
$new_column_headers['column_1'] = 'Contact Name';
$column_headers = array_insert_after($column_headers, 'shipping_company', $new_column_headers);
return $column_headers;
}
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns' );
您只需执行以下操作即可将 'column_1' 设置或添加为第一列:
add_filter( 'wc_customer_order_csv_export_order_headers', 'sv_wc_csv_export_reorder_columns', 10, 2 );
function sv_wc_csv_export_reorder_columns( $column_headers, $csv_generator ) {
// Save "column_1" in a new array
$column1 = array('column_1' => __("Contact Name") );
// Remove "column_1" to be reordered
unset( $column_headers['column_1'] );
// Remove unnecessary "delivery_date"
unset( $column_headers['delivery_date'] );
// Set "column_1" as first column merging arrays
return $column1 + $column_headers;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。