Home › Toolset Professional Support › [Resolved] Split: Custom View to display Upsell/Related Products – display upsell and related product when upcell is product is less than eight
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.
This topic is split from https://toolset.com/forums/topic/custom-view-to-display-upsell-related-products/
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 8 replies, has 2 voices.
Last updated by Minesh 1 year, 1 month ago.
Assisted by: Minesh.
Is there any way I could have a View of 8 Upsell/Related products where the upsell products are displayed first and then, if room for related products, those are added?
Like 3 upsell products and then 5 related products inside the grid of 2x4? Now the 8 related products are dangling a bit silly below the upsell products.
Hello. Thank you for contacting the Toolset support.
Can you please check now: hidden link
I've added the following custom shortcode to "Custom Code" section offered by Toolset:
=> hidden link
add_filter('wpv_filter_query', 'func_show_upsell_related_products', 99, 3); function func_show_upsell_related_products( $query_args, $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 ); $upsell_count = count($upsell_ids); if( $upsell_count < $min_num_of_products_display ) { $rel_products = get_view_query_results( 3937 ); /// 3937 is the related product view ID $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]->ID; } $query_args['post__in'] = array_merge($upsell_ids,$related_ids); $query_args['post__not_in'] = null; $query_args['orderby'] = 'post__in'; } } return $query_args; }
And then I've edit your existing elementor product template:
- hidden link
And removed the related product view widget from your elementor template and adjusted the code added to content template as given under:
=> hidden link
[wpv-view name="view-upsell-products" ]
Can you please confirm it works as expected now.
More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
- https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results
Thank you for providing that code.
2 issues I run into:
- on a product with upsell items hidden link, could you reverse the order so that the upsell products come first and after those the remaining related products? Upsells are more important than related items.
- on a product without upsell items hidden link, the page is broken due to the snippet if I insert either [upsells] or [wpv-post-body view_template="upsell-product-view-container"]
2 issues I run into:
- on a product with upsell items hidden link, could you reverse the order so that the upsell products come first and after those the remaining related products? Upsells are more important than related items.
===>
I've adjusted the code added to "Custom Code" section as given under:
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 ); $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 $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 } } return $query; }
- on a product without upsell items hidden link, the page is broken due to the snippet if I insert either [upsells] or [wpv-post-body view_template="upsell-product-view-container"]
===>
I've removed the shortcode added to your elementor template [upsells] and I can see now page is loaded as expected.
- hidden link
Glad to help and have a great weekend.
Products without Upsell or Related items, like hidden link or hidden link are still broken. Probably because that is the only product in that category.
Can you please check now.
I've adjusted the code added to "Custom Code" section as given under:
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 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 } } } return $query; }
I hope the provided solution should work as expected now.
Wow, excellent, thank you!
I need to transfer all settings to the live site, so I guess I'll have to edit the view_id to reflect the ID on the live site.
Also, do I still need "upsells" under Functions inside conditional evaluations?
Yes - you will have to adjust the $view_id and view ID that returns the related product IDs:
$rel_products = get_view_query_results( 3937 ); /// 3937 is the related product view ID
Also, do I still need "upsells" under Functions inside conditional evaluations?
==>
No - its not required now.