Skip Navigation

[Resolved] Assign WordPress Category automatically by selected Product Category

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that creates Product posts. Two taxonomies are assigned to Products: Product Categories and standard Categories. When a Product is created by Forms, Users will define the Product Categories. I would like to programmatically assign the same terms in the standard Category taxonomy.

Solution: You can manipulate the terms associated with a new post created by Forms using the Forms API cred_save_data. Add this code to your child theme's functions.php file or create a new snippet in Toolset > Settings > Custom Code:

add_action('cred_save_data', 'ts_copy_to_cats_from_pcats',10,2);
function ts_copy_to_cats_from_pcats($post_id, $form_data) {
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    $post_terms = wp_get_object_terms($post_id, 'product_cat', array('fields' => 'slugs'));
    wp_set_object_terms($post_id, $post_terms, 'category', false);
  }
}

Replace 12345 with the numeric ID of the New Product Form.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_get_object_terms
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

This support ticket is created 6 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Nashaat 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1140404

I would like to know how to do following:

i have created a form to submit woocommerce products, where the user can chose a product category before submitting the product (product_cat).

now i have also assigned the built-in wordpress Category to products post type, this means products now have both (product category and normal wordpress category)

both of these Categories have the same terms. for example:

Product categories: category1, category2, category3, category4, category5
normal categories: category1, category2, category3, category4, category5

when submitting the form of a new product i would like that the built-in category terms get filled out too depending on what the user has chosen in the product category

this should be like following :

1- user has chosen category1, category3
2- user submits the form
3- product category has now two terms category1, category3
4- normal category has also the same two terms category1, category3

why i want to do this?

I have many post types that use the built-in category. and i have a search form for all of the post types together with products too, so the problem is if i want to filter all post types by category term then only regular post types can get filtered and woocommerce product wont show up in the results because they use the Product category (product_cat) and note (category).

one solution is to use the regular category and thats it. but i i would like to keep both categories for the reason if i needed the product category in the future.

is that possible how can i do this?

thank you

#1140656

Hi, you can manipulate the terms associated with a new post created by Forms using the Forms API cred_save_data. Add this code to your child theme's functions.php file or create a new snippet in Toolset > Settings > Custom Code:

add_action('cred_save_data', 'ts_copy_to_cats_from_pcats',10,2);
function ts_copy_to_cats_from_pcats($post_id, $form_data) {
  $forms = array( 12345 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    $post_terms = wp_get_object_terms($post_id, 'product_cat', array('fields' => 'slugs'));
    wp_set_object_terms($post_id, $post_terms, 'category', false);
  }
}

Replace 12345 with the numeric ID of the New Product Form.

#1141044

This exactly did the thing! thank you Christian i appreciate your support.