Snippets: Display WooCommerce Category Thumbnail as Page Header Background

WooCommerce thumbnails are used by default for the category grid/listing. Because of this it’s not used as the title backgrounds by default, however, it can be enabled via a child theme function.

function myprefix_page_header_style( $style ) {
	if ( function_exists( 'is_product_category' ) && is_product_category() ) {
		global $wp_query;
		$cat = $wp_query->get_queried_object();
		if ( $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ) ) {
			$style = 'background-image';
		}
	}
	return $style;
}
add_filter( 'wpex_page_header_style', 'myprefix_page_header_style' );

function myprefix_woo_cat_page_header_bg( $image ) {
	if ( function_exists( 'is_product_category' ) && is_product_category() ) {
		global $wp_query;
		$cat = $wp_query->get_queried_object();
		if ( $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ) ) {
			$image = $thumbnail_id; 
		}
	}
	return $image;
}
add_filter( 'wpex_page_header_background_image', 'myprefix_woo_cat_page_header_bg' );
All PHP snippets should be added via a child theme's functions.php file.