I import a VERY large number of products into Woocommerce from XML files with a PHP script using the wp_insert_post() and add_post_meta() functions.
It works well but it's very slow, averaging 60 products per minute.
I've tried running multiple instances of my script at the same time (each instance processing different XML files), but it doesn't make things any better.
However, in the process management of my dedicated server, the processor and memory resources are not at all exploited to the maximum.
the initial code that does the insert :
$post_id = wp_insert_post(array(
'post_excerpt' => $resume,
'post_title' => $titre_produit,
'post_status' => 'publish', //pending
'post_type' => 'product'
));
// affectation de la catégorie par défaut
$uncategorized_term_id = get_option( 'default_product_cat' );
$product = wc_get_product($post_id);
$product->set_category_ids([ $uncategorized_term_id ] );
$product->save();
add_post_meta($post_id, '_collection', $collection);
add_post_meta($post_id, '_width', $largeur);
add_post_meta($post_id, '_height', $hauteur);
add_post_meta($post_id, '_length', $longueur);
add_post_meta($post_id, '_regular_price', $price);
add_post_meta($post_id, '_price', $price);
add_post_meta($post_id, '_tva', $tax);
add_post_meta($post_id, '_nombrepages', $nbPages);
add_post_meta($post_id, '_editor', $editor);
add_post_meta($post_id, '_date', $publish_dateTxt);
add_post_meta($post_id, '_author', $autors);
add_post_meta($post_id, '_sku', $sku);
add_post_meta($post_id, '_productForm', $ProductForm);
add_post_meta($post_id, '_ProductAvailability', $ProductAvailability);
if($link_image != '') {
fifu_dev_set_image($post_id, $link_image);
}
Following the comments given to me here I modified the script to use new WC_Product_Simple() and $product->set_ :
$product = new WC_Product_Simple();
$product->set_name( $titre_produit );
$product->set_description( $resume );
$product->set_sku( $sku );
$product->set_regular_price( $price );
$product->set_height( $hauteur );
$product->set_width( $largeur );
$product->set_length( $longueur );
$product_id = $product->get_id();
// affectation de la catégorie par défaut
$uncategorized_term_id = get_option( 'default_product_cat' );
$product = wc_get_product($product_id);
$product->set_category_ids([ $uncategorized_term_id ] );
$product->add_meta_data('_collection', $collection);
$product->add_meta_data('_tva', $tax);
$product->add_meta_data('_nombrepages', $nbPages);
$product->add_meta_data('_editor', $editor);
$product->add_meta_data('_date', $publish_dateTxt);
$product->add_meta_data('_author', $autors);
$product->add_meta_data('_productForm', $ProductForm);
$product->add_meta_data('_ProductAvailability', $ProductAvailability);
if($link_image != '') {
fifu_dev_set_image($product_id, $link_image);
}
$product->save();
I works but it does not improve the speed, on the contrary it is now about 30% slower.
I upgraded the server from 2 to 12 processors and allowed a maximum of memory to PHP, but I gain practically nothing on the insertion of products (it goes from 5 to 6 per minute...)
I also tested the custom CRUD function given in answer here but it doesn't improve the speed.
Is there really no way to speed up this process?
Thanks !