Home›Toolset Professional Support›[Resolved] Split: Need help to display individual product from each subcategory in View Loop – get product ID from term custom fields
[Resolved] Split: Need help to display individual product from each subcategory in View Loop – get product ID from term custom fields
This thread is resolved. Here is a description of the problem and solution.
Problem:
Display the related product based on the product ID stored in the term custom field Solution:
You will require to use the Toolset View's filter hook "wpv_filter_query" in order to modify the view's query on fly.
Hi, I am sorry to bother you again Minesh,
But there was a misunderstanding of mine. My boss actually had asked a different query rule and displaying the similar products.
And correct Process to display the similar products is that, each subcategory has a list of Products Custom ids which are saved in a custom field called "product-id" and What my boss wants is
I have displayed the Excel file that includes the product IDs connected with Each Category. So Basically we want to first add a query filter which will be the "Current Category of the page where view is displayed" and after that, I need to check all connected Product IDs with the current Category and once the system finds available Product IDs then it will pull only 1 Product from each product ID And display them as similar products, For example, there are 12 Product IDs for that category then 12 Products will be displayed as Similar Products. I have explained all these details in the video. So please watch it and please, please help me to make it work. Its really important feature.
hidden link
All Credentials for access are still the same. you can access the site.
Hello. Thank you for contacting the Toolset support.
As I can see the product IDs are attach to the specific term and you are having it on excel sheet.
To attach the product IDs to term - you will require to create a term custom field (numeric type) and set this field to have multiple instances (repeating field) and you can then add your product IDs to as per the excel sheet to your term when you edit your term.
Okay, I have added the custom field in terms with Repeated options. and also added all the IDs in each subcategory. But as you had mentioned in above comment to add numeric value, so that will not work for some IDs because some IDs are having Dash(-) characters so numeric field will give an error.
Please check the site now and help me to make it work.
I've added the following filter to "Custom Code" section offered by Toolset:
function func_products_attach_to_term( $query_args ,$view_settings, $view_id ) {
global $post;
if ( $view_id == 6685 ) {
// getting current term info attached to post
$terms = wp_get_object_terms( $post->ID,'product_cat');
$include_ids = array();
// getting product IDs attached to term & search and get related post belongs to product ID
foreach($terms as $k=>$v):
$product_ids = get_term_meta($v->term_id,'wpcf-term_ids');
foreach($product_ids as $x=>$y):
$args = array('post_type' => 'product',
's' => $y,
'post_status' => 'publish',
'orderby' => 'rand'
);
$result = new WP_Query($args);
// adding one product out of found products
$include_ids[] = $result->posts[0]->ID;
endforeach;
endforeach;
if(empty($include_ids)){
$include_ids = array(0);
}
// passing product IDs
$query_args['post__in'] = $include_ids;
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'func_products_attach_to_term', 10, 3);
Can you please confirm it works as expected and mark resolve both the tickets. 🙂