Issue with product variations after data migration

  Kiến thức lập trình

After a data migration of nearly 6k products from the live site. We are currently experiencing some issues with some of the products. We have contacted the plugin developers of the plugins used and they could see the issue. After they performed their necessary checks (switching themes and disabling plugins) they concluded that the issue was a generic woocommerce issue.

I’ve found an manual fix but it will take ages to complete this task manually. So I looked on SO to find if anybody has the same or smiliar issue and found this post : Woocommerce: function to update all products the fix didn’t work.

Below is my code from my functions.php. I’ve added some debug statements to see how long the process takes to process 200 products

 if (!function_exists('write_log'))
    function write_log($log)
        if (true === WP_DEBUG)
            if (is_array($log) || is_object($log))
                error_log(print_r($log, true));

add_action( 'init', 'update_products_by_x' );

function update_products_by_x()
    $limit = 200;

    // getting all products
    $products_ids = get_posts( array(
        'post_type'        =>  ['product','product_variation'],
        'numberposts'      => $limit,
        'post_status'      => 'publish',
        'fields'           => 'ids',
        'meta_query'       => array( array(
           'key'     => '_sync_updated',
           'compare' => 'NOT EXISTS',
        ) )
    ) );

    if( count($products_ids) > 0 )
        $randomId = wp_rand();
        $current_date_time_start = current_datetime()->format('Y-m-d H:i:s');
        $log_products_message_start = $randomId . " | " . $current_date_time_start . " | function: update_products_by_x | Start";

        // Loop through product Ids
        foreach ( $products_ids as $product_id ) {

            // Get the WC_Product object
            $product = wc_get_product($product_id);

            // Mark product as updated
            $product->update_meta_data( '_sync_updated', true );


        $current_date_time_end = current_datetime()->format('Y-m-d H:i:s');
        $log_products_message_end = $randomId . " | " . $current_date_time_end . " | function: update_products_by_x | End ";
        write_log("update_products_by_x has no products to process");

Manual Fix

Within the variations tab I unticked “Enabled” on all the variations, then pressed the saved changes. Then I clicked the update button.

I then checked the frontend for the product and the product was now “out of stock” and did not display any of the options.

Went back into the variations and the ticked “Enabled” on all the variations then pressed the saved changes and press the update.

Checked the product again and now all the options are back and when selected they update the selected text.

The client was trying this fix but they were using the “Bulk Actions” and “toggle ‘Enabled’” instead of going into each individual and amended “Enabled” tick box but that didn’t fix the issue. So doing the individual changes saves all the variation attributes and this somehow resolves the issue.

Please can someone help in converting the manual process into a automated process.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website