-
AuthorPosts
-
December 16, 2015 at 2:17 pm #709671
DanielParticipantI’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?
December 16, 2015 at 6:59 pm #709996
Rue NelModeratorHello 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.
December 22, 2015 at 9:04 am #717479
DanielParticipantHi,
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.
December 22, 2015 at 10:12 am #717573
ThaiModeratorHi 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 🙂
December 22, 2015 at 11:35 am #717690
DanielParticipantHi,
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.
December 22, 2015 at 6:16 pm #718069
LelyModeratorHello There,
Glad it is working now. Thank you for updating the thread.
Always,
X -
AuthorPosts
- <script> jQuery(function($){ $("#no-reply-709671 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>
