This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Setting the 1st Gallery Image as Default Drop down image

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Setting the 1st Gallery Image as Default Drop down image Reply To: Setting the 1st Gallery Image as Default Drop down image

#37533
Ernest MarcinkoErnest Marcinko
Keymaster

I think I actually managed to find a possible solution. I did a bit of research and placed this custom code to the functions.php file in your theme directory:

// Ajax Search Pro - Getting the first WooCommerce gallery image as the featured image
add_filter( 'asp_results', 'asp_get_woocommerce_gallery_first_image', 10, 1 );
function asp_get_woocommerce_gallery_first_image( $results ) {
  foreach ($results as $k=>&$r) { 
    if ( isset($r->post_type) && $r->post_type == 'product' ) {
		$product = wc_get_product($r->id);
		$attachment_ids = $product->get_gallery_image_ids();
		if ( isset($attachment_ids[0]) ) {
			$r->image = wp_get_attachment_url( $attachment_ids[0] );
		}
    } 
  } 
  return $results;
}

From what I can see, where the gallery is available, I am seeing the first image as the results featured image.