Skip Navigation

[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.

You can find the proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/split-need-help-to-display-individual-product-from-each-subcategory-in-view-loop-get-product-id-from-term-custom-fields/#post-1788771

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 3 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

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 7 replies, has 2 voices.

Last updated by manishB-2 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1788397

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.

#1788403

Minesh
Supporter

Languages: English (English )

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

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.

Can you please do that and get back to me.

#1788633

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.

#1788655

Minesh
Supporter

Languages: English (English )

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

Ok - so you can change that field to text field instead of numeric field. Once you done, please let me know.

#1788671

I have already done that. Added text field and also added all ids to all sub categories. You check in there.

#1788771

Minesh
Supporter

Languages: English (English )

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

Great.

Can you please check now: hidden link

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. 🙂

#1789893

Minesh
Supporter

Languages: English (English )

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

Can you please confirm that the solution I shared help you to resolve your issue.

#1790137

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.