Skip to content

Instantly share code, notes, and snippets.

@Acephalia
Last active February 10, 2024 12:17
Show Gist options
  • Save Acephalia/49cb4a3b97a3399637e023f434c50245 to your computer and use it in GitHub Desktop.
Save Acephalia/49cb4a3b97a3399637e023f434c50245 to your computer and use it in GitHub Desktop.
Swap Woocommerce Product Thumbnails With First Gallery Image
//Swap Woocommerce product thumbnail with first image in gallery by u/acephaliax
//This snippet will swap out the product thumbnail for all archive and shop loops to display first galery image but show product image as main image on a single product page.
add_filter('woocommerce_product_get_image', 'custom_woocommerce_product_get_image', 10, 5);
function custom_woocommerce_product_get_image($image, $product, $size, $attr, $placeholder) {
if (is_shop() || is_product_category() || is_product_tag()) { // Adjust condition as needed
$gallery_images = $product->get_gallery_image_ids();
if (!empty($gallery_images)) {
$image_id = $gallery_images[0]; // Sets first gallery image as thumbnail
$image = wp_get_attachment_image($image_id, $size, false, $attr);
}
}
return $image;
}
@Acephalia
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment