Skip to content

Instantly share code, notes, and snippets.

@PeterKnight
Created July 14, 2013 17:10
Show Gist options
  • Save PeterKnight/5994961 to your computer and use it in GitHub Desktop.
Save PeterKnight/5994961 to your computer and use it in GitHub Desktop.
This is example code that will hide the image markup for Woocommerce products if a product does not have a thumbnail. Useful if you are displaying a service for example that doesn't have a product image.
function before_no_image_product() {
if( !has_post_thumbnail( get_the_id() ) ){
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
echo '<div class="no-image">';
}
}
function after_no_image_product() {
if( !has_post_thumbnail( get_the_id() ) ){
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
echo '</div>';
}
}
function before_no_image_single_product() {
if( !has_post_thumbnail( get_the_id() ) ){
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
echo '<div class="no-image">';
}
}
function after_no_image_single_product_image_restore() {
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
if( !has_post_thumbnail( get_the_id() ) ){
echo '</div>';
}
}
add_action( 'woocommerce_before_shop_loop_item', 'before_no_image_product', 9 );
add_action( 'woocommerce_after_shop_loop_item', 'after_no_image_product', 9 );
add_action( 'woocommerce_before_single_product_summary', 'after_no_image_single_product', 30 );
add_action( 'woocommerce_after_single_product_summary', 'after_no_image_single_product_image_restore', 30 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment