Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #709671
    Daniel
    Participant

    I’ve set a default image in woocommerce using woocommerce supplied code:

    add_action( 'init', 'custom_fix_thumbnail' );
     
    function custom_fix_thumbnail() {
      add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
       
    	function custom_woocommerce_placeholder_img_src( $src ) {
    	$upload_dir = wp_upload_dir();
    	$uploads = untrailingslashit( $upload_dir['baseurl'] );
    	$src = $uploads . '/2012/07/thumb1.jpg';
    	 
    	return $src;
    	}
    }

    in child theme functions.php

    However, I’ve noticed that X does not use this image in the “related products” section. It uses it for the main product image, but it looks like the image X relies on in related products is the product’s “featured” image, set on the product edit page. Is there a way to change this so it falls back to the default image if a featured image has not been set?

    #709996
    Rue Nel
    Moderator

    Hello There,

    Thanks for posting in!

    Checking your code and it seems it doesn’t have a correct format. Please update your code and use this instead:

    function custom_woocommerce_placeholder_img_src( $src ) {
      $upload_dir = wp_upload_dir();
      $uploads = untrailingslashit( $upload_dir['baseurl'] );
      $src = $uploads . '/2012/07/thumb1.jpg';
      return $src;
    }
    
    function custom_fix_thumbnail() {
      add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
    }
    add_action( 'init', 'custom_fix_thumbnail' ); 

    We would loved to know if this has work for you. Thank you.

    #717479
    Daniel
    Participant

    Hi,

    Thanks for the code, but it has not worked. It actually caused the default image on the single product page to break, with no improvement in the “related products” section.

    #717573
    Thai
    Moderator

    Hi Daniel,

    Please try with this code instead:

    add_action( 'init', 'x_custom_fix_thumbnail' );
     
    function x_custom_fix_thumbnail() {
    	add_filter( 'woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src' );
    	add_filter( 'post_thumbnail_html', 'default_woo_img', 20, 2 );
    	function default_woo_img($html, $product_id){
    		$post_type = get_post_type( get_post( $product_id ) );
    		if( $post_type == "product" && !has_post_thumbnail($product_id) ){
    			$upload_dir = wp_upload_dir();
    			$uploads    = untrailingslashit( $upload_dir['baseurl'] );
    			$src        = $uploads . '/2012/07/thumb1.jpg';
    			return '<img src="'.$src.'" />';
    		} else {
    			return $html;
    		}
    	}
    	function custom_woocommerce_placeholder_img_src( $src ) {
    		$upload_dir = wp_upload_dir();
    		$uploads    = untrailingslashit( $upload_dir['baseurl'] );
    		$src        = $uploads . '/2012/07/thumb1.jpg';
    
    		return $src;
    	}
    }

    Hope it helps 🙂

    #717690
    Daniel
    Participant

    Hi,

    So using this code (btw, you were missing a closing bracket) plus the original code in my 1st post, works. Is this what you expected? So the entire code block needed to work correctly is:

    add_action( 'init', 'custom_fix_thumbnail' );
     
    function custom_fix_thumbnail() {
      add_filter('woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src');
       
    	function custom_woocommerce_placeholder_img_src( $src ) {
    	$upload_dir = wp_upload_dir();
    	$uploads = untrailingslashit( $upload_dir['baseurl'] );
    	$src = $uploads . '/2015/12/cocktails-731898_640.jpg';
    	 
    
    	 
    	return $src;
    	}
    }
    
    add_action( 'init', 'x_custom_fix_thumbnail' );
    
     
    function x_custom_fix_thumbnail() {
    	add_filter( 'woocommerce_placeholder_img_src', 'custom_woocommerce_placeholder_img_src' );
    	
    	add_filter( 'post_thumbnail_html', 'default_woo_img', 20, 2 );
    	
    	function default_woo_img($html, $product_id){
    		$post_type = get_post_type( get_post( $product_id ) );
    		if( $post_type == "product" && !has_post_thumbnail($product_id) ){
    			$upload_dir = wp_upload_dir();
    			$uploads    = untrailingslashit( $upload_dir['baseurl'] );
    			$src        = $uploads . '/2015/12/cocktails-731898_640.jpg';
    			return '<img src="'.$src.'" />';
    		} 
    		
    		else {
    			return $html;
    		}
    	}
    }

    Without the original code the single product page main image throws a PHP error.

    #718069
    Lely
    Moderator

    Hello There,

    Glad it is working now. Thank you for updating the thread.

    Always,
    X

  • <script> jQuery(function($){ $("#no-reply-709671 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>