Deleting WooCommerce Product Images by Attribute: A Safe Approach

In a recent Business Bloomer Club thread, a member encountered an issue while attempting to remove images from WooCommerce products with a specific attribute. The function they initially wrote ended up deleting images from all products, which wasn’t the intended outcome. This post explores how to safely delete product images based on specific attributes and ensure that only products matching the criteria are affected.

The user’s goal was to remove images only from products with a particular attribute, such as the brand. However, the initial code used wc_get_products with unsupported parameters (attribute and attribute_term), resulting in an error that applied the action to all products. Below is a revised version of the function, which ensures only products with a specific attribute are affected by first checking for the attribute term in each product.

Updated Solution for Targeted Image Deletion

To avoid unintended deletions, here’s how you can safely remove product images only for products with a specific attribute term:

function remove_images_from_products() {
    $args = array(
        'status' => 'publish',
        'limit' => -1,
    );

    $products = wc_get_products($args);

    foreach ($products as $product) {
        $product_id = $product->get_id();
        $taxonomy = 'pa_marque'; // Replace with your attribute taxonomy
        $terms = get_the_terms($product_id, $taxonomy);

        // Check if the product has the specific term
        if (!empty($terms) && $terms[0]->term_id == 25) { // Replace 25 with the desired term ID
            $featured_image_id = $product->get_image_id();
            $image_galleries_id = $product->get_gallery_image_ids();

            if (!empty($featured_image_id)) {
                wp_delete_attachment($featured_image_id, true);
            }

            if (!empty($image_galleries_id)) {
                foreach ($image_galleries_id as $single_image_id) {
                    wp_delete_attachment($single_image_id, true);
                }
            }
        }
    }
}

How This Solution Works

  1. Retrieve All Products: The wc_get_products function fetches all published products.
  2. Check Product Attributes: For each product, the code retrieves the attribute terms using get_the_terms. It only proceeds if the term matches the desired attribute ID.
  3. Delete Images: For matching products, the function deletes both featured and gallery images by their attachment IDs.

Final Thoughts

Deleting product images by attribute requires careful handling to avoid unintended data loss. This function ensures safe and specific targeting of products based on attributes. Always test functions like these on a staging site before deploying them in a live environment.

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Reply

Your email address will not be published. Required fields are marked *