我正在使用WooCommerce客户/订单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 - 如何设置它以使其出现在此列之前或将其设置为第一列?
您只需以下内容即可设置或添加"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;
}
代码进入函数.php活动子主题(或活动主题)的文件。经过测试并工作。
试试这段代码
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' );