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


Below is sample code demonstrating how to add new features, specifically custom fields support, to WooCommerce's built-in CSV importer by extending its functionality through hooks and filters. This sample shows how to register additional fields to be imported and saved with the product post meta during the import process.

php
// Hook into WooCommerce product CSV importer to add custom columns in the importer UI
add_filter('woocommerce_product_importer_parsed_data', 'custom_woocommerce_product_importer_parsed_data', 10, 2);
function custom_woocommerce_product_importer_parsed_data($parsed_data, $data) {
    // Example: Add custom field 'custom_text_field' from CSV column 'custom_text'
    if (!empty($data['custom_text'])) {
        $parsed_data['meta_data'][] = array(
            'key'   => '_custom_text_field',
            'value' => sanitize_text_field($data['custom_text']),
        );
    }
    // Example: Add custom field 'custom_number_field' from CSV column 'custom_number'
    if (!empty($data['custom_number'])) {
        $parsed_data['meta_data'][] = array(
            'key'   => '_custom_number_field',
            'value' => floatval($data['custom_number']),
        );
    }
    return $parsed_data;
}

// Hook to add custom CSV columns headers to the importer UI
add_filter('woocommerce_product_importer_csv_columns', 'custom_woocommerce_product_importer_csv_columns');
function custom_woocommerce_product_importer_csv_columns($columns) {
    $columns['custom_text'] = __('Custom Text', 'your-textdomain');
    $columns['custom_number'] = __('Custom Number', 'your-textdomain');
    return $columns;
}

// Save custom fields after product import/update
add_action('woocommerce_product_import_inserted_product_object', 'custom_save_imported_product_meta', 10, 1);
function custom_save_imported_product_meta($product) {
    $imported_meta_data = $product->get_meta_data();
    foreach ($imported_meta_data as $meta) {
        // Save custom meta keys defined above
        if (in_array($meta->key, array('_custom_text_field', '_custom_number_field'))) {
            $product->update_meta_data($meta->key, $meta->value);
        }
    }
    $product->save();
}

Additional instructions and details for extending the WooCommerce importer with custom fields:

- The filter `woocommerce_product_importer_parsed_data` receives parsed CSV row data and allows injection of additional meta data before product saving. This is where custom field values from CSV columns can be added to the product.
- The filter `woocommerce_product_importer_csv_columns` lets you add new custom columns to the CSV import mapping screen so users can match CSV columns to your custom fields.
- The action `woocommerce_product_import_inserted_product_object` is triggered after each product is created or updated during import, useful to finalize saving the custom metadata.
- Custom meta keys should start with an underscore `_` to be treated as hidden post meta by WordPress conventions unless you want them publicly visible.
- Sanitize and validate data when injecting custom fields to enhance security and data integrity.
- This example handles simple text and number fields, but can be extended to handle serialized data, arrays, or other complex structures depending on the custom meta usage.
- For importing complex custom data like attributes, variations, or custom taxonomies, additional processing will be required beyond this simple meta injection.
- Consider compatibility with WooCommerce versions and test importing workflows to handle edge cases gracefully.

This approach keeps the core WooCommerce importer intact while enhancing it through WordPress hooks to support custom features in the import process tailored to specific requirements.