Reputation: 137
In WooCommerce, I have to insert 25 000 product from a json file!
I was thinking using the WPDB()
insert()
method, because When using WC_Product()
methods, it's a heavier process that takes more time and ressources on the server.
So in the below code I am trying to use WPDB()
insert()
method:
for( $i = 0; $i < count($data->DataList); $i++ ) {
$DiamondData = array(
'Shape' => $Shape,
'Size' => $Size,
'Color' => $Color,
'Clarity' => $Clarity,
'Cut' => $Cut
);
$wpdb->insert($table,$DiamondData);
$my_id = $wpdb->insert_id;
}
Any help and guidance will be really appreciated.
Upvotes: 1
Views: 1090
Reputation: 254378
In your code example, it seems that you are trying to add some product attributes to each product. Product attributes are something complicated that requires to check if they exists and if the terms exits too. If they don't , you need to create them… Once done you can set them in the product.
Using WPDB()
class insert()
method in a foreach loop, means that you are inserting data product by product, and it's going also to be very heavy too regarding the number of products and the things that need to be checked.
But you are not obliged to use WC_Product()
Class and methods. You can also use the old way using Wordpress functions and there is many examples on Stack Overflow.
So you could use something like below, that will insert simple products with their product attributes, but limiting the process to 500 products each time, for example.
If the process is stopped for any reason, you can restart it where it was…
The code (that you can embed in a function):
$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
}
Make a database backup…
You might need to complete the code for other data that need to be set in each product.
Upvotes: 2