【问题标题】:WooCommerce Customer/Order CSV Export - Add Column as first columnWooCommerce 客户/订单 CSV 导出 - 添加列作为第一列
【发布时间】:2019-02-01 08:13:27
【问题描述】:

我正在使用 WooCommerce 客户/订单 CSV 导出并在我的函数文件中有一个 sn-p(从 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 - 我如何设置它以使其出现在此列之前或将其设置为第一列?

【问题讨论】:

    标签: php wordpress woocommerce export orders


    【解决方案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 文件中。测试和工作。

    【讨论】:

      【解决方案2】:

      试试这个代码

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

      【讨论】:

        猜你喜欢
        • 2016-12-23
        • 2020-08-29
        • 2022-01-20
        • 1970-01-01
        • 2018-09-29
        • 2019-07-15
        • 1970-01-01
        • 2019-03-15
        • 2018-09-15
        相关资源
        最近更新 更多