Louis
Louis

Reputation: 2608

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.

Upvotes: 2

Views: 2907

Answers (2)

Devin
Devin

Reputation: 1

The SQLLite version worked for me perfectly. Thank you! Saved me the time of manually removing variation images from over 500 products! For anyone who wants to know, this is what I did.

  1. logged into the WP back-end.
  2. Found the functions.php file through Appearance>Theme Editor>functions.php (You can select the file by clicking on the name from the list onto the left. (in my case)
  3. Scrolled all the way to the bottom, and pasted the function that @LoicTheAztec has given, just within the php code.
  4. Saved the file and refreshed the site. Voila! Images gone!
  5. Removed the function so that it doesn't keep running, and saved the file again.

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253814

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.

Upvotes: 3

Related Questions