Skip to content

Instantly share code, notes, and snippets.

@andyg2
Created November 23, 2023 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyg2/d14333e3920f39954ab5a2bdf1963f1b to your computer and use it in GitHub Desktop.
Save andyg2/d14333e3920f39954ab5a2bdf1963f1b to your computer and use it in GitHub Desktop.
Override the WooCommerce subcategory thumbnail to fallback to a product image or the standard fallback image
<?php
/*
Plugin Name: WC Category Image Fallback
Description: WooCommerce Product Image Fallback for Categories.
Version: 1.0
Author: Andy Gee
*/
/**
* Override the WooCommerce subcategory thumbnail to fallback to a product image or the standard fallback image.
*
* @param object $category The category object.
* @return void
*/
function override_woocommerce_subcategory_thumbnail($category) {
// Get the small thumbnail size for subcategories
$small_thumbnail_size = apply_filters('subcategory_archive_thumbnail_size', 'woocommerce_thumbnail');
// Get the dimensions of the small thumbnail size
$dimensions = wc_get_image_size($small_thumbnail_size);
// Get the thumbnail ID of the category
$thumbnail_id = get_term_meta($category->term_id, 'thumbnail_id', true);
// Initialize the image variable
$image = null;
// If the category has a thumbnail
if ($thumbnail_id) {
// Get the image source and set it to the image variable
$image = wp_get_attachment_image_src($thumbnail_id, $small_thumbnail_size);
$image = $image[0];
// Get the image srcset and sizes attributes if the corresponding functions exist
$image_srcset = function_exists('wp_get_attachment_image_srcset') ? wp_get_attachment_image_srcset($thumbnail_id, $small_thumbnail_size) : false;
$image_sizes = function_exists('wp_get_attachment_image_sizes') ? wp_get_attachment_image_sizes($thumbnail_id, $small_thumbnail_size) : false;
} else {
// Get all the products in the category
$products = wc_get_products([
'category' => get_term($category->term_id, 'product_cat')->slug
]);
// If there are products in the category
if (!empty($products)) {
// Loop through the products
foreach ($products as $product) {
// Get the image ID of the product
$image_id = $product->get_image_id();
// If the product has an image ID
if ($image_id) {
// Get the image source and set it to the image variable
$image = wp_get_attachment_image_src($image_id, $small_thumbnail_size);
$image = $image[0];
// Get the image srcset and sizes attributes if the corresponding functions exist
$image_srcset = function_exists('wp_get_attachment_image_srcset') ? wp_get_attachment_image_srcset($image_id, $small_thumbnail_size) : false;
$image_sizes = function_exists('wp_get_attachment_image_sizes') ? wp_get_attachment_image_sizes($image_id, $small_thumbnail_size) : false;
break;
}
}
}
// If no image is found, use the WooCommerce placeholder image
if (!$image) {
$image = wc_placeholder_img_src();
$image_srcset = false;
$image_sizes = false;
}
}
// If an image is found
if ($image) {
// Replace spaces in the image URL with '%20'
$image = str_replace(' ', '%20', $image);
// Output the image with the appropriate attributes
if ($image_srcset && $image_sizes) {
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr($category->name) . '" width="' . esc_attr($dimensions['width']) . '" height="' . esc_attr($dimensions['height']) . '" srcset="' . esc_attr($image_srcset) . '" sizes="' . esc_attr($image_sizes) . '" />';
} else {
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr($category->name) . '" width="' . esc_attr($dimensions['width']) . '" height="' . esc_attr($dimensions['height']) . '" />';
}
}
}
/**
* WooCommerce category thumbnails don't have a filter.
* Remove the original action and add a custom function.
*/
remove_action('woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail', 10);
add_action('woocommerce_before_subcategory_title', 'override_woocommerce_subcategory_thumbnail', 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment