0

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.

WHM process manager

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 !

Anatole
  • 15
  • 4
  • 1
    What have you tried to resolve the problem? Where are you stuck? As you haven't shared a single line of code, it's impossible for other to check **why** your script is slow – Nico Haase Jun 22 '23 at 07:05
  • What I tried = run multiple instances at the same time. Where am I stuck = I don't know how to improve the execution speed. – Anatole Jun 22 '23 at 11:13
  • The difference is that some WooCommerce functions use that WooCommerce database tables for some faster search queries and analytics quick display, for example. Sooner, Products will be partially migrated to those WooCommerce tables. In your code, you should move `$product->save()` at the end of your code to allow WooCommerce Sync data. – LoicTheAztec Jun 22 '23 at 14:05
  • Thanks for the explanation. I will therefore have to redo part of my script, but I may not have time for that. I will probably have to delegate this part to do it well and have a faster script. – Anatole Jun 22 '23 at 15:41
  • I modified my script using new WC_Product_Simple() and $product->set_ but it does not improve the speed, on the contrary it is now about 30% slower. – Anatole Jun 24 '23 at 08:17
  • Try posting it on Database Administrators site – Rohit Gupta Jul 05 '23 at 04:00
  • @RohitGupta OK but where is it ? – Anatole Jul 05 '23 at 08:09

0 Answers0