skip to Main Content

How do I set a default product thumbnail on WooCommerce even if there is an existing product image on a product, but will not replace or change the existing one on the database? Hope someone can help, thank you in advance.

2

Answers


  1. Chosen as BEST ANSWER

    This one worked for me:

    function alterImageSRC($image, $attachment_id, $size, $icon) {
            
        $image[0] = 'http://newimagesrc.com/myimage.jpg';
    
        return $image;
    }
    
    add_filter('wp_get_attachment_image_src', 'alterImageSRC', 10, 4);
    

  2. All right so let me explain the solution for you. You have to remove product loop original image thumbnail with the remove action. Then you have to insert new action which is calling the custom thumbnail. Don’t forget to replace URL OF YOUR IMAGE HERE with your image URL. For a single product you have to do the same. This code goes into functions.php file of your theme. Tested and works.

    //Product loop
    
    function custom_image_woo() {
        // Remove original product image from product loop
      remove_action( 'woocommerce_before_shop_loop_item_title', 'custom_image_woo', 10 );
        // Your custom image thumbnail function
        function here_is_the_magic() {
            echo '<img src="URL OF YOUR IMAGE HERE">';
        }
        add_action( 'woocommerce_before_shop_loop_item_title', 'here_is_the_magic', 10 );
    }
    add_action( 'woocommerce_init', 'custom_image_woo');
    
    // Single product page
    
    //this removes product featured image
    
    add_filter('woocommerce_single_product_image_thumbnail_html', 'main_image_away', 10, 2);
    function main_image_away($html, $attachment_id ) {
        global $post, $product;
        $featured_image = get_post_thumbnail_id( $post->ID );
        if ( $attachment_id == $featured_image )
            $html = '';
        return $html;
    }
    
    //this will add your image before gallery
    
    function gallery(){
        echo '<img src="URL OF YOUR IMAGE HERE">';
    }
    add_action( 'woocommerce_product_thumbnails', 'gallery', 10 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search