WooCommerce客户/订单CSV导出-添加列为第一列

时间:2019-02-01 08:13:27

标签: php wordpress woocommerce export orders

我正在使用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-如何设置它使其出现在此列之前或将其设置为第一列?

2 个答案:

答案 0 :(得分: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;
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。

答案 1 :(得分:0)

尝试此代码

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' );