Recently I was creating a store for one of my customer. Then He asked me to insert some products as a starting base for his shop. I did not want to do it manually, and I did not want to use an external plugin to do that for me. I wanted to import them via some custom PHP script that I can build upon and extend.

Here is the script I found on Stack Overflow. It is a good starting point for further development. Hope you will find it useful as well:

$limit = 500; // Number of products to be processed (here 500 by 500 products)

$index = (int) get_option( 'custom_product_insertion_index' ); // Get the index of processed products
$insertion_count = 0; // Initializing

// Loop through data array to be inserted in each product
for( $i = $index; $i < count($data->DataList); $i++ ) {
    // First insert the new product to get the post ID
    $post_id = wp_insert_post(
      'post_title' => $product_name,
      'post_content' => $product_description,
      'post_type' => 'product',
      'post_status' => 'publish' 
    );

    // Set the product type (here a simple product)
    wp_add_object_terms( $post_id, 'simple', 'product_type' );

    $attributes_data = [];

    $attributes = array(
        'Shape'   => $shape,
        'Size'    => $size,
        'Color'   => $color,
        'Clarity' => $clarity,
        'Cut'     => $cut
    );

    $count = 0;

    // Check if attributes and terms exist, if not we create them
    foreach ($attributes_raw as $attribute => $term ){
        $taxonomy = 'pa_'.sanitize_title($attribute_name); // The attribute taxonomy

        // If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil)
        if( ! taxonomy_exists( $taxonomy ) ){
            register_taxonomy(
                $taxonomy,
               'product_variation',
                array(
                    'hierarchical' => false,
                    'label' => ucfirst($taxonomy),
                    'query_var' => true,
                    'rewrite' => array( 'slug' => sanitize_title($attribute_name)), // The base slug
                ),
            );
        }

        $term_name = ucfirst($term);

        // Add the product attribute term if it doesn't exist.
        if( ! term_exists( $term_name, $taxonomy ) )
            wp_insert_term( $term_name, $taxonomy ); // Create the term

        // Set the term in the product
        wp_set_post_terms( $post_id, $term_name, $taxonomy );

        $attributes_data[$taxonomy] = array(
            'name'         => $taxonomy,
            'value'        => '',
            'position'     => $count,
            'is_visible'   => true,
            'is_variation' => false,
            'is_taxonomy'  => true,
        );
        $count++;
    }

    // Add the product attribute data in the product
    update_post_meta($post_id, '_product_attributes', $attributes_data );

    // Add the product price
    update_post_meta($post_id, '_price', $price );

    // and so on…

    $insertion_count++; 

    // Saving the number of processed products
    update_option( 'custom_product_insertion_index', $index++ );

    if( $insertion_count >= 500 ) break; // stop the loop after 500 products inserted
}

Before playing with WP database and inserting any data, always make backup first. Never work on production app. Always create a development copy and only after testing create backup and run your script on production.

https://stackoverflow.com/questions/55661829/bulk-insert-simple-products-in-woocommerce-using-wpdb-methods

0
Would love your thoughts, please comment.x
()
x