Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Sample code to import product categories in WooCommerce
    woocommerce import data


Sample code to import product categories in WooCommerce


Here's a sample PHP code to import product categories in WooCommerce:

php
<?php
// Make sure WooCommerce is active
if (!function_exists('WC')) {
    return;
}

// Function to import categories
function import_woocommerce_categories($categories) {
    foreach ($categories as $category) {
        $parent_id = 0;
        
        // Check if this is a subcategory
        if (strpos($category, '>') !== false) {
            $cat_parts = explode('>', $category);
            $parent_slug = trim($cat_parts[0]);
            $category_name = trim($cat_parts[1]);
            
            // Get or create parent category
            $parent_term = term_exists($parent_slug, 'product_cat');
            if ($parent_term !== 0 && $parent_term !== null) {
                $parent_id = $parent_term['term_id'];
            } else {
                $parent_term = wp_insert_term($parent_slug, 'product_cat');
                $parent_id = $parent_term['term_id'];
            }
        } else {
            $category_name = trim($category);
        }
        
        // Check if category already exists
        $term = term_exists($category_name, 'product_cat', $parent_id);
        if ($term !== 0 && $term !== null) {
            echo "Category '$category_name' already exists.\n";
        } else {
            $result = wp_insert_term($category_name, 'product_cat', array('parent' => $parent_id));
            if (is_wp_error($result)) {
                echo "Error creating category '$category_name': " . $result->get_error_message() . "\n";
            } else {
                echo "Category '$category_name' created successfully.\n";
            }
        }
    }
}

// Sample category data
$categories_to_import = array(
    'Electronics',
    'Electronics > Smartphones',
    'Electronics > Laptops',
    'Clothing',
    'Clothing > Men',
    'Clothing > Women',
    'Clothing > Women > Dresses'
);

// Run the import
import_woocommerce_categories($categories_to_import);

This code does the following:

1. It checks if WooCommerce is active.

2. It defines a function `import_woocommerce_categories` that takes an array of categories.

3. For each category:
- It checks if it's a subcategory (contains '>')
- If it's a subcategory, it creates or gets the parent category first
- It then creates the category (or subcategory) if it doesn't already exist

4. It provides a sample array of categories to import, including some with hierarchies.

5. Finally, it calls the import function with the sample data.

To use this code:

1. Ensure WooCommerce is installed and activated.
2. Replace the `$categories_to_import` array with your actual category data.
3. Run this script in your WordPress environment (e.g., as part of a plugin or theme, or via WP-CLI).

Remember to always backup your database before running import scripts. Also, for large datasets, you might need to adjust PHP execution time and memory limits.

This script provides a basic structure for importing categories. You can extend it to handle more complex scenarios, such as importing category descriptions, images, or custom fields.

Citations:
[1] https://www.webtoffee.com/woocommerce-import-product-categories/
[2] https://wpdesk.net/blog/woocommerce-import-categories/
[3] https://avada.io/woocommerce/docs/import-categories.html
[4] https://www.wpallimport.com/documentation/how-to-import-woocommerce-products-csv-xml/
[5] https://stackoverflow.com/questions/62375207/woocommerce-import-products-with-subcategories