Last active
February 10, 2024 12:17
-
-
Save Acephalia/49cb4a3b97a3399637e023f434c50245 to your computer and use it in GitHub Desktop.
Swap Woocommerce Product Thumbnails With First Gallery Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Snippet for reddit request : https://www.reddit.com/r/divi/comments/1alq7o3/placement_of_woo_product_pictures_in_divi/