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

问题描述

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

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

现在,我想向每个订单项添加一些额外的数据。例如,项目的作者(在我们的情况下为卖方)。 我找到了一种将exta数据添加到导出中的好方法。可以在插件的文档中找到: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',// Address
                'vendor_company'            => get_the_author_Meta( '_wcv_custom_settings_company_name','vendor_street'             => get_the_author_Meta( '_wcv_store_address1','vendor_housenumber'        => get_the_author_Meta( '_wcv_store_address2','vendor_zip'                => get_the_author_Meta( '_wcv_store_postcode','vendor_city'               => get_the_author_Meta( '_wcv_store_city','vendor_country'            => get_the_author_Meta( '_wcv_store_country',// Bank
                'vendor_bank_name'          => get_the_author_Meta( 'wcv_bank_name','vendor_bank_account_name'  => get_the_author_Meta( 'wcv_bank_account_name','vendor_iban'               => get_the_author_Meta( 'wcv_bank_iban','vendor_bic'                => get_the_author_Meta( 'wcv_bank_bic_swift',);
        }


        return sv_wc_csv_export_add_custom_order_data( $order_data,$custom_data,$csv_generator );
}
add_filter( 'wc_customer_order_export_csv_order_row','sv_wc_csv_export_modify_row_data_example',10,3 );

我猜我将数据添加错误的位置?! 但是我不知道问题出在哪里。

编辑:@Cbroe发表评论后,我尝试使用wc_customer_order_csv_export_order_line_item

在这里找到了一个示例,但是它破坏了我的CSV文件

/**
 * Add weight as line item Meta in CSV export Default format
 */
function sv_add_weight_to_csv_export_line_item( $line_item,$item,$product,$order ) {

    $line_item['weight'] = $product->get_weight();

    return $line_item;
}
add_filter( 'wc_customer_order_csv_export_order_line_item','sv_add_weight_to_csv_export_line_item',4 );

/**
 * Add weight as line item Meta in CSV export CSV Import format
 */
function sv_add_weight_to_csv_export_import_format( $order_data,$order ) {

    $count = 1;

    // add line items
    foreach ( $order->get_items() as $item ) {

        $product = $order->get_product_from_item( $item );

        if ( ! is_object( $product ) ) {
            $product = new WC_Product( 0 );
        }

        if ( $weight = $product->get_weight() ) {
            $order_data[ "order_item_{$count}" ] .= '|weight: ' . $weight;
        }

        $count++;
    }

    return $order_data;
}
add_filter( 'wc_customer_order_csv_export_order_row','sv_add_weight_to_csv_export_import_format',20,2 );

但我仍在尝试...

解决方法

我找到了答案。这是一个隐藏在官方文档中的示例:https://github.com/skyverge/wc-plugins-snippets/blob/master/woocommerce-customer-order-coupon-export/csv/add-item-meta-to-order-export.php

/**
 * Add line item meta to the Order CSV Export in Default format
 * Example: add weight to the item meta data
 */


/**
 * Step 1. Add weight to line item data
 *
 * @param array $line_item the original line item data
 * @param array $item the item's order data
 * @param object $product the \WC_Product object for the line
 * @param object $order the \WC_Order object being exported
 * @return array the updated line item data
 */
function sv_wc_csv_export_add_weight_to_line_item( $line_item,$item,$product,$order ) {

    $new_item_data = array();

    foreach ( $line_item as $key => $data ) {

        $new_item_data[ $key ] = $data;

        if ( 'sku' === $key && $product ) {
            $new_item_data['weight'] = wc_format_decimal( $product->get_weight(),2 );
        }
    }

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


/**
 * (optional) Step 2. Add a separate `item_weight` column to the default and custom formats
 *
 * @param array $column_headers the original column headers
 * @param \CSV_Export_Generator $csv_generator the generator instance
 * @return array - the updated column headers
 */
function sv_wc_csv_export_modify_column_headers_item_price( $column_headers,$csv_generator ) {

    $new_headers = array();

    if ( sv_wc_csv_export_is_one_row( $csv_generator ) ) {

        foreach( $column_headers as $key => $column ) {

            $new_headers[ $key ] = $column;

            // add the item_price after the SKU column
            if ( 'item_sku' === $key ) {
                $new_headers['item_weight'] = 'item_weight';
            }
        }

    } else {

        return $column_headers;
    }

    return $new_headers;
}
add_filter( 'wc_customer_order_export_csv_order_headers','sv_wc_csv_export_modify_column_headers_item_price',2 );


/**
 * (optional) Step 3. Add the item weight data (step 1) to the new item_weight column (step 2)
 * for the Default - One Row per Item format
 *
 * @param array $order_data the original order data
 * @param array $item       the item for this row
 * @return array - the updated order data
 */
function sv_wc_csv_export_order_row_one_row_per_item_weight( $order_data,$item ) {

    $order_data['item_weight'] = $item['weight'];
    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',2 );


if ( ! function_exists( 'sv_wc_csv_export_is_one_row' ) ) :

/**
 * Helper function to check the export format
 *
 * @param \WC_Customer_Order_CSV_Export_Generator $csv_generator the generator instance
 * @return bool - true if this is a one row per item format
 */
function sv_wc_csv_export_is_one_row( $csv_generator ) {

    $one_row_per_item = false;

    if ( version_compare( wc_customer_order_csv_export()->get_version(),'4.0.0','<' ) ) {

        // pre 4.0 compatibility
        $one_row_per_item = ( 'default_one_row_per_item' === $csv_generator->order_format || 'legacy_one_row_per_item' === $csv_generator->order_format );

    } elseif ( isset( $csv_generator->format_definition ) ) {

        // post 4.0 (requires 4.0.3+)
        $one_row_per_item = 'item' === $csv_generator->format_definition['row_type'];
    }

    return $one_row_per_item;
}

endif;