Skip Navigation

[Resolved] Upsell/Related snippet no longer filtering the products

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by kristofG 1 year, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2650327

Hi Minesh,

You have greatly helped us with this snippet, but today, it does not seem to work anymore.
https://toolset.com/forums/topic/split-custom-view-to-display-upsell-related-products-display-upsell-and-related-product-when-upcell-is-product-is-less-than-eight/

This item hidden link should have no upsell and no related products because it is the only product in that category.

We have another ticket with Waqar where the Select dropdowns no longer popuplate with the relevant options and instead show all options. Maybe this is related?

#2650329

Ok, after some more testing. It seems the Related Products are indeed from the same Category as the Archive shown but ONLY IF there are related products in that category, otherwise, the related products are just randomly chosen although the View "view-related-products" is set to "Product categories the same as the current post in the loop".

1/ Could you tweak the code that if there are no upsell/related products, the view stays empty?

2/ Related to this, you had tweaked the code because when there are no related products, the snippet broke the page. This is happening again, example hidden link

#2650397

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I've again adjusted the code as per your requirement that when there is no related products and upsell products it should not display any products:

add_filter('wpv_filter_query_post_process', 'func_show_upsell_related_products', 99, 3);
function func_show_upsell_related_products( $query, $view_settings, $view_id ) {
	global $post;
   
    if($view_id == 3680 ){
       
      $min_num_of_products_display = 8;
      $upsell_count = 0;

      $upsell_ids = get_post_meta( $post->ID, '_upsell_ids', true );

      if(empty($upsell_ids)) { $upsell_ids = array(0);}

      $args = array(
                  'post_type'		=> 'product',
                  'post_status'	=> 'publish',
                  'post__in'		=> $upsell_ids
              );
      $upsell_posts = get_posts( $args );

      $upsell_count = count($upsell_posts);

      if( $upsell_count <  $min_num_of_products_display ) {

          $rel_products = get_view_query_results( 3937 ); /// 3937 is the related product view ID
          $rel_products_count = count($rel_products);
        
        
          if($rel_products_count < $min_num_of_products_display){
            $min_num_of_products_display = $rel_products_count;
          }
          

         if(!empty($rel_products)) {
              $get_related_count = $min_num_of_products_display - $upsell_count;
              
              $related_ids = array();
              for($i=0;$i<$get_related_count;$i++){
                  $related_ids[]=$rel_products[$i];
              }

              $total_found_posts = array_merge($upsell_posts,$related_ids);

              $query->posts = $total_found_posts; // add the default post to the posts result array
              $query->found_posts = count($total_found_posts); // modify the count of found posts
              $query->post_count = $total_found_posts; // modify the count of displayed posts

          }else{
           	  $query->posts = array(); // add the default post to the posts result array
              $query->found_posts = 0; // modify the count of found posts
              $query->post_count = 0; // modify the count of displayed posts
          }

        }

	}
    return $query;
}

Can you please check now its working as expected for both the cases you shared.

#2650411

Excellent work Minesh, thank you!

Is there a way to hide the View when the result is empty? Now the title "This could also interest you - "no items found" looks a bit silly. Ideally we would hide the whole view/output.

#2650415

Ok, so I found https://toolset.com/forums/topic/hide-entire-view-if-empty/ and I used the same thinking, placing the H2 inside the View Items Found instead of fixed in the Elementor Template.