WooCommerce 订单导出:获取每个行项目的自定义数据

我正在使用“WooCommerce 客户/订单/优惠券导出”插件将我的订单导出为 CSV 文件。

我正在以每项一行的格式导出订单。这意味着每个订单都有多行。每个订单项目一行。

现在我想向每个行项目添加一些额外的数据。例如该项目的作者(在我们的例子中是供应商)。我找到了一种向导出添加额外数据的好方法。它可以在插件的文档中找到:https://docs.woocommerce.com/document/ordercustomer-csv-export-developer-documentation/#section-14

它有效,但并不完全是我所需要的。我可以向整个订单添加一些数据。因此每个行项目都会获得相同的数据。

所以我尝试更改代码并检查每个行项目。不幸的是,结果相同。每个行项目都获取相同的数据。就我而言,是订单最后一项的数据。

以下是我如何更改官方文档中示例的第 2 步:

function sv_wc_csv_export_modify_row_data_example( $order_data, $order, $csv_generator ) {


        // Example showing how to extract order metadata into it's own column

        //$meta_key_example = is_callable( array( $order, 'get_meta' ) ) ? $order->get_meta( 'meta_key_example' ) : $order->meta_key_example;

        //$meta_key_example = is_callable( array( $order, 'get_meta' ) ) ? $order->get_meta( 'meta_key_example' ) : $order->meta_key_example;



        // Loop through order line items


        $allItems = $order->get_items();


        foreach( $allItems as $item_line ){


            $item_vendor_id     = get_post_field( 'post_author', $item_line->get_product_id() );


            $custom_data = array(



                // User/Vendor

                'item_vendor_id'            => $item_vendor_id,

                'vendor_username'           => get_the_author_meta( 'username', $item_vendor_id ),

                'vendor_user_email'         => get_the_author_meta( 'user_email', $item_vendor_id ),


                );

        }



        return sv_wc_csv_export_add_custom_order_data( $order_data, $custom_data, $csv_generator );

}

我猜我在错误的位置添加了数据?!但我无法弄清楚问题出在哪里。


撒科打诨
浏览 198回答 1
1回答

鸿蒙传说

我找到了答案。这是隐藏在官方文档中的一个示例:https ://github.com/skyverge/wc-plugins-snippets/blob/master/woocommerce-customer-order-coupon-export/csv/add-item-meta-to-订单导出.php/**&nbsp;* Add line item meta to the Order CSV Export in Default format&nbsp;* Example: add weight to the item meta data&nbsp;*//**&nbsp;* Step 1. Add weight to line item data&nbsp;*&nbsp;* @param array $line_item the original line item data&nbsp;* @param array $item the item's order data&nbsp;* @param object $product the \WC_Product object for the line&nbsp;* @param object $order the \WC_Order object being exported&nbsp;* @return array the updated line item data&nbsp;*/function sv_wc_csv_export_add_weight_to_line_item( $line_item, $item, $product, $order ) {&nbsp; &nbsp; $new_item_data = array();&nbsp; &nbsp; foreach ( $line_item as $key => $data ) {&nbsp; &nbsp; &nbsp; &nbsp; $new_item_data[ $key ] = $data;&nbsp; &nbsp; &nbsp; &nbsp; if ( 'sku' === $key && $product ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_item_data['weight'] = wc_format_decimal( $product->get_weight(), 2 );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $new_item_data;}add_filter( 'wc_customer_order_export_csv_order_line_item', 'sv_wc_csv_export_add_weight_to_line_item', 10, 4 );/**&nbsp;* (optional) Step 2. Add a separate `item_weight` column to the default and custom formats&nbsp;*&nbsp;* @param array $column_headers the original column headers&nbsp;* @param \CSV_Export_Generator $csv_generator the generator instance&nbsp;* @return array - the updated column headers&nbsp;*/function sv_wc_csv_export_modify_column_headers_item_price( $column_headers, $csv_generator ) {&nbsp; &nbsp; $new_headers = array();&nbsp; &nbsp; if ( sv_wc_csv_export_is_one_row( $csv_generator ) ) {&nbsp; &nbsp; &nbsp; &nbsp; foreach( $column_headers as $key => $column ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_headers[ $key ] = $column;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add the item_price after the SKU column&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( 'item_sku' === $key ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $new_headers['item_weight'] = 'item_weight';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; return $column_headers;&nbsp; &nbsp; }&nbsp; &nbsp; return $new_headers;}add_filter( 'wc_customer_order_export_csv_order_headers', 'sv_wc_csv_export_modify_column_headers_item_price', 10, 2 );/**&nbsp;* (optional) Step 3. Add the item weight data (step 1) to the new item_weight column (step 2)&nbsp;* for the Default - One Row per Item format&nbsp;*&nbsp;* @param array $order_data the original order data&nbsp;* @param array $item&nbsp; &nbsp; &nbsp; &nbsp;the item for this row&nbsp;* @return array - the updated order data&nbsp;*/function sv_wc_csv_export_order_row_one_row_per_item_weight( $order_data, $item ) {&nbsp; &nbsp; $order_data['item_weight'] = $item['weight'];&nbsp; &nbsp; return $order_data;}add_filter( 'wc_customer_order_export_csv_order_row_one_row_per_item', 'sv_wc_csv_export_order_row_one_row_per_item_weight', 10, 2 );if ( ! function_exists( 'sv_wc_csv_export_is_one_row' ) ) :/**&nbsp;* Helper function to check the export format&nbsp;*&nbsp;* @param \WC_Customer_Order_CSV_Export_Generator $csv_generator the generator instance&nbsp;* @return bool - true if this is a one row per item format&nbsp;*/function sv_wc_csv_export_is_one_row( $csv_generator ) {&nbsp; &nbsp; $one_row_per_item = false;&nbsp; &nbsp; if ( version_compare( wc_customer_order_csv_export()->get_version(), '4.0.0', '<' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // pre 4.0 compatibility&nbsp; &nbsp; &nbsp; &nbsp; $one_row_per_item = ( 'default_one_row_per_item' === $csv_generator->order_format || 'legacy_one_row_per_item' === $csv_generator->order_format );&nbsp; &nbsp; } elseif ( isset( $csv_generator->format_definition ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // post 4.0 (requires 4.0.3+)&nbsp; &nbsp; &nbsp; &nbsp; $one_row_per_item = 'item' === $csv_generator->format_definition['row_type'];&nbsp; &nbsp; }&nbsp; &nbsp; return $one_row_per_item;}endif;
打开App,查看更多内容
随时随地看视频慕课网APP