This is only for technically experienced users.
WooCommerce has introduced some changes to how order items are created. Previously, the SKU was automatically added to the order item meta fields. However, this meta field is no longer inserted automatically, and the value is now read from the product each time the edit order page is accessed.
As we know, many users may need the SKU field for their order exports. We have created a code snippet that should include this field value in the exported files.
Please ensure the following fields are included in the export template for this code to function properly:
- SKU
- _variation_id
- _product_id
To get it working, please follow these instructions:
-
Navigate to Your WordPress Server Installation: Go to wp-content > themes > your-child-theme.
-
If you are unfamiliar with child themes, you can learn more by visiting the WordPress Codex.
-
-
Locate the functions.php File: Search for this file within your child theme's folder.
-
Edit the File: Open the functions.php file using your preferred text editor program.
- Add the following code.
if ( is_admin() ) {
add_filter( 'atum/export/atum_export_entity/post_export_process', function ( $do_post_export_process, $entity_name ) {
if ( 'shop_order' === $entity_name || 'shop_order_hpos' === $entity_name ) {
$do_post_export_process = TRUE;
}
return $do_post_export_process;
}, 10, 2 );
add_filter( 'atum/export/atum_export_entity/export_row', function ( $row, $params, $entity ) {
if ( 'shop_order' === $entity->get_name() || 'shop_order_hpos' === $entity->get_name() ) {
$data_export = $entity->get_data_export();
if ( isset( $data_export->structure['order_items']['data']['meta']['SKU'] ) ) {
$product_id = isset( $row['_variation_id'] ) ? $row['_variation_id'] : 0;
if ( ! $product_id && isset( $row['_product_id'] ) ) {
$product_id = $row['_product_id'];
}
if ( $product_id ) {
$product = wc_get_product( $product_id );
if ( $product instanceof \WC_Product ) {
$row['SKU'] = $product->get_sku();
}
}
}
}
return $row;
}, 10, 3 );
}
-
Save the File: After making the necessary changes, save the file to ensure all modifications are updated.
-
Export from the Export Center: Proceed to the Export Center to export your data with the newly included SKU field.