Speeding Up Bulk Product Creation (With Variations)
-
Hi,
I am creating variable products. An Example, I have 9 colors, 9 sizes. Which makes 9×9 = 81 total variations for 1 product.Foreach variation, below is the function that runs.
function create_product_variation( $product_id, $variation_data, $productColorsAndIDs ){ // Get the Variable product object (parent) $product = wc_get_product($product_id); $variation_post = array( 'post_title' => $product->get_title(), 'post_name' => 'product-'.$product_id.'-variation', 'post_status' => 'publish', 'post_parent' => $product_id, 'post_type' => 'product_variation', 'guid' => $product->get_permalink() ); // Creating the product variation $variation_id = wp_insert_post( $variation_post ); // Get an instance of the WC_Product_Variation object $variation = new WC_Product_Variation( $variation_id ); // Iterating through the variations attributes foreach ($variation_data['attributes'] as $attribute => $term_name ) { //Only have 2 attributes, size and color. $taxonomy = 'pa_'.$attribute; update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_name ); } // Prices $variation->set_price( $variation_data['regular_price'] ); $variation->set_regular_price( $variation_data['regular_price'] ); $variation->set_image_id($variation_data['variation_thumbnail_id']); $variation->save(); // Save the data }
Before running the above code, I encapsulate the loop as below:
wp_defer_term_counting( false); //Speeding Up Bulk Update Tricks wp_defer_comment_counting( false ); //Speeding Up Bulk Update Tricks //Here comes the loop, which calls the above function create_product_variation(). wp_defer_term_counting( true ); wp_defer_comment_counting( true );
The creation process is slow even I am on a fast hosting (SiteGround GoGeek hosting plan.) Above code creates 1 product in 1-2 minutes, which is pretty slow, and most of the time, I get gateway 504 errors while running it.
What am I missing here ? How can I optimize it to work faster ?
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Speeding Up Bulk Product Creation (With Variations)’ is closed to new replies.