Search code examples
phpsqlwordpresswoocommerceproduct-variations

Bulk remove product variation images in Woocommerce


in Woocommerce (3.5.1 in my case), is it possible to remove all variation-specific images, so the image doesn't change when the user chooses a variation in the product page ?

I want this

enter image description here

instead of that

enter image description here in the whole site.


Solution

  • You can bulk remove all product variation images bulk making a direct SQL query using phpMyAdmin for example (before backup your database):

    UPDATE wp_postmeta as pm
    JOIN wp_posts AS p ON pm.post_id = p.ID
    SET pm.meta_value = ''
    WHERE p.post_status = 'publish'
    AND p.post_type = 'product_variation'
    AND pm.meta_key = '_thumbnail_id'
    

    Or you can use this light SQL query in a custom built-in function to remove all the variations images (or optionally only to a related parent variable product ID):

    function remove_all_variations_images( $parent_id = 0 ){
        global $wpdb;
    
        $one_parent = $parent_id === 0 ? "" : "AND p.post_parent = $parent_id";
    
        return $wpdb->query("
            UPDATE {$wpdb->prefix}postmeta as pm
            JOIN {$wpdb->prefix}posts AS p ON pm.post_id = p.ID
            SET pm.meta_value = ''
            WHERE p.post_status = 'publish'
            AND p.post_type = 'product_variation'
            AND pm.meta_key = '_thumbnail_id' 
            $one_parent
        ");
    }
    

    Code goes in function.php file of your active child theme (or active theme).

    USAGE (before backup your database):

    1. For all variations images in bulk you will add to your function.php file:

      remove_all_variations_images();
      

      Then save and browse any page of your site. Then remove it once finish.


    1. For one specific variable product (let say variable product ID 72):

      remove_all_variations_images(72);
      

      Then save and browse any page of your site. Then remove it once finish.