Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Give sample code to add new features in WooCommerce's built-in importer


Give sample code to add new features in WooCommerce's built-in importer


Here is some sample code to add new features to WooCommerce's built-in product CSV importer:

php
// Add custom column to import mapping screen
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_custom_column_to_importer' );
function add_custom_column_to_importer( $columns ) {
  $columns['my_custom_field'] = 'My Custom Field';
  return $columns;
}

// Map custom column to product meta field
add_filter( 'woocommerce_csv_product_import_mapping_defined_fields', 'map_custom_column_to_product_meta' );
function map_custom_column_to_product_meta( $mapping ) {
  $mapping['my_custom_field'] = 'meta:my_custom_field';
  return $mapping;
}

// Import custom product meta field
add_action( 'woocommerce_product_import_inserted_product_object', 'import_custom_product_meta', 10, 2 );
function import_custom_product_meta( $product, $data ) {
  if ( isset( $data['my_custom_field'] ) ) {
    $product->update_meta_data( 'my_custom_field', sanitize_text_field( $data['my_custom_field'] ) );
  }
}

This code does a few things:

1. It adds a new column called "My Custom Field" to the import mapping screen using the `woocommerce_csv_product_import_mapping_default_columns` filter[1]. This allows the user to map a column in their CSV to this custom field.

2. It maps the "My Custom Field" column to the `meta:my_custom_field` product meta field using the `woocommerce_csv_product_import_mapping_defined_fields` filter[1]. This tells the importer to import the values from that column into the `_my_custom_field` product meta field.

3. It hooks into the `woocommerce_product_import_inserted_product_object` action to actually import the custom product meta field[1]. If the `my_custom_field` key is set in the import data, it updates the product meta with the sanitized value from the CSV.

To use this, you would:

1. Add this code to your theme's `functions.php` file or a custom plugin.

2. In your CSV, include a column header called "my_custom_field" (or whatever you named it in the code).

3. Map the "My Custom Field" column to the `meta:my_custom_field` field in the import mapping screen.

4. Run the import. The custom field values will be imported into the `_my_custom_field` product meta field.

This is just a basic example, but it demonstrates how you can extend the built-in importer to handle custom fields and meta data. The key is using the provided filters and actions to hook into the import process at the right points.

Citations:
[1] https://woocommerce.com/document/product-csv-importer-exporter/
[2] https://stackoverflow.com/questions/47041693/woocommerce-csv-import-custom-fields-adjust-built-in-importer-to-include-custo
[3] https://diviengine.com/downloading-and-importing-woocommerce-sample-data/
[4] https://woocommerce.com/document/code-snippets/
[5] https://www.plytix.com/blog/import-product-data-into-woocommerce