我正在尝试自定义WooCommerce CSV Export 插件为两个标准产品属性(“基金”和“上诉代码”)添加额外的列。
我可以添加实际的标题,但无法获取行中的任何数据。下面是我正在尝试的代码:
// add custom column headers
function wc_csv_export_modify_column_headers( $column_headers ) {
$new_headers = array(
\'fund\' => \'fund\',
\'appeal\' => \'appeal_code\',
// add other column headers here in the format column_key => Column Name
);
return array_merge( $column_headers, $new_headers );
}
add_filter(
\'wc_customer_order_csv_export_order_headers\',
\'wc_csv_export_modify_column_headers\'
);
// set the data for each for custom columns
function wc_csv_export_modify_row_data( $order_data, $order, $csv_generator ) {
$custom_data = array(
\'fund\' => wc_get_product_terms( $product->id, \'pa_fund\' ),
\'appeal\' => wc_get_product_terms( $product->id, \'pa_appeal-code\' ),
// add other row data here in the format column_key => data
);
$new_order_data = array();
if ( isset( $csv_generator->order_format )
&& ( \'default_one_row_per_item\' == $csv_generator->order_format
|| \'legacy_one_row_per_item\' == $csv_generator->order_format
)
) {
foreach ( $order_data as $data ) {
$new_order_data[] = array_merge( (array) $data, $custom_data );
}
} else {
$new_order_data = array_merge( $order_data, $custom_data );
}
return $new_order_data;
}
add_filter(
\'wc_customer_order_csv_export_order_row\',
\'wc_csv_export_modify_row_data\',
10,
3
);
最合适的回答,由SO网友:dpruth 整理而成
在一位知识更渊博的同事的帮助下,我们成功了。代码如下:
/** Add custom column headers **/
function wc_csv_export_modify_column_headers( $column_headers ) {
$new_headers = array(
\'fund\' => \'fund\',
\'appeal\' => \'appeal_code\',
);
return array_merge( $column_headers, $new_headers );
}
add_filter( \'wc_customer_order_csv_export_order_headers\',
\'wc_csv_export_modify_column_headers\' );
/** Set the data for each for custom columns **/
function wc_csv_export_modify_row_data( $order_data, $item, $order, $csv_generator ) {
// Determine the product ID from the SKU, and use that to find the fund and appeal code
$pid = wc_get_product_id_by_sku($item[sku]);
$fund = wc_get_product_terms( $pid, \'pa_fund\' );
$appeal = wc_get_product_terms( $pid, \'pa_appeal-code\' );
// Add the fund & appeal code to the order_data & return each line
$order_data["fund"] = $fund[0];
$order_data["appeal"] = $appeal[0];
return $order_data;
}
add_filter( \'wc_customer_order_csv_export_order_row_one_row_per_item\',
\'wc_csv_export_modify_row_data\', 10, 4 );