Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Questions about plugin setup / features
This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko 3 years, 3 months ago.
- AuthorPosts
- February 20, 2020 at 1:51 pm #25899
1. How to show in the search results the image of a parent product if the image for a variation is not set?
2. How to exclude products for which catalog visibility is set to hidden from indexing?
3. Is it possible to specify the image size for the alternative image source based on a custom field (similar to the featured image)? https://share.getcloudapp.com/P8uRd4G0February 20, 2020 at 3:01 pm #25900Hi,
1. It might be possible via using a custom code only. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
add_filter( 'asp_results', 'asp_get_main_prod_img', 10, 1 ); function asp_get_main_prod_img($results) { foreach ( $results as $k => &$r ) { if ( isset($r->post_type) && $r->post_type == 'product_variation' && empty($r->image) ) { $wc_prod_var_o = wc_get_product( $r->id ); $img = wp_get_attachment_image_src( get_post_thumbnail_id($wc_prod_var_o->get_parent_id()), 'single-post-thumbnail' ); if (isset($img, $img[0]) && !is_wp_error($img)) $r->image = $img[0]; } } return $results; }
2. This knowledge base should help you out.
Best,
3. Unfortunately no, as usually those images are uploaded only in a single dimention. There is however an automatic image cropper script, that you can enable, and will try to crop the images as close as possible to the size of the results output: https://i.imgur.com/BqgCzDQ.png
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
February 20, 2020 at 3:44 pm #259011. Thanks!
2. Thanks, I saw it and it works, but as far as I understand this is not about indexing, products hidden from the catalog will still be included into index. This is not important actually as the current solution works fine.
3. Those images are uploaded with ACF in my case and have the [‘sizes’] array with all image sizes. Maybe there is a way to access this array and assign (hard-code) the medium size to images of a specific custom post type?February 21, 2020 at 4:21 pm #25931Hi,
3. Well, we can actually try a modification to the previous code:
add_filter( 'asp_results', 'asp_get_main_prod_img', 10, 1 ); function asp_get_main_prod_img($results) { $acf_image_field = 'image_field_name'; $size = 'single-post-thumbnail'; foreach ( $results as $k => &$r ) { if ( isset($r->post_type) && $r->post_type == 'product_variation' && empty($r->image) ) { $wc_prod_var_o = wc_get_product( $r->id ); $img = wp_get_attachment_image_src( get_post_thumbnail_id($wc_prod_var_o->get_parent_id()), 'single-post-thumbnail' ); if (isset($img, $img[0]) && !is_wp_error($img)) $r->image = $img[0]; } if ( empty($r->image) && function_exists('get_field') ) { $image_id = get_field($acf_image_field, $r->id); if ( !empty($image_id) ) { $img = wp_get_attachment_image_src( $image_id, 'single-post-thumbnail' ); if (isset($img, $img[0]) && !is_wp_error($img)) $r->image = $img[0]; } } } return $results; }
Important:
Best,
– Change the $acf_image_field and the $size variables at the beginning of the function
– Remove the custom field source from the image sources – so that the code can handle those images programatically
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
February 25, 2020 at 2:48 pm #25985Thanks, Ernest! The example you provide works great if in the ACF settings the return format is set to ‘Image ID’. In my case it was ‘Image Array’ so I modified the code a bit:
add_filter( 'asp_results', 'asp_get_main_prod_img', 10, 1 ); function asp_get_main_prod_img($results) { $acf_image_field = 'design_image'; $size = 'medium'; foreach ( $results as $k => &$r ) { if ( empty($r->image) && function_exists('get_field') ) { $image_array = get_field( $acf_image_field, $r->id ); if ( count( $image_array ) > 0 && array_key_exists( 'sizes', $image_array ) ) { $img = $image_array['sizes'][$size]; if ( isset( $img ) && !is_wp_error( $img ) ) $r->image = $img; } } } return $results; }
Thank you for the help!
February 25, 2020 at 3:53 pm #25993You are very welcome, that solution looks handy, at least I know more about the ACF image field 🙂
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
You must be logged in to reply to this topic.