The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Populate Formidable field with products from Woocommerce product category

Labels

This Discussion is public

Notifications

Hi all. After browsing with no luck on finding the quick solution, i thought i would post mine. i hope this is the place to do so. This seemed to work for me because i needed multiple forms (1 per each parent category for my products) that would populate my field with ONLY the products in the specific category. Please feel free to correct this if there are any errors.

 

**** Add the following code to your functions.php file
or create a new php file and stick it in wp-content/mu-plugins/ folder: ***


 

Example 1 - Only one form with products from a single product category:

add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_posts', 20, 2); //use this function on edit too
function frm_populate_posts($values, $field){
if($field->id == 125){ //replace 125 with the ID of the field to populate
$posts = get_posts( array('post_type' => 'product', 'product_cat' => 'PRODUCT-CATEGORY-SLUG', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
unset($values['options']);
$values['options'] = array(''); //remove this line if you are using a checkbox or radio button field
foreach($posts as $p){
$values['options'][$p->ID] = $p->post_title;
}
$values['use_key'] = true; //this will set the field to save the post ID instead of post title
}

return $values;
}


 

Example 2 - With multiple forms (or form fields) from multiple product categories:
Note: copy the orange text as many times as you need depending on how many product categories and form fields you need to populate.

 

// FIRST PRODUCT
add_filter('frm_setup_new_fields_vars', 'frm_populate_posts', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_posts', 20, 2); //use this function on edit too
function frm_populate_posts($values, $field){
if($field->id == 125){ //replace 125 with the ID of the field to populate
$posts = get_posts( array('post_type' => 'product', 'product_cat' => 'PRODUCT-CATEGORY-SLUG', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
unset($values['options']);

foreach($posts as $p){
$values['options'][$p->ID] = $p->post_title;
}
$values['use_key'] = true; //this will set the field to save the post ID instead of post title
}

//SECOND PRODUCT
if($field->id == 126){ //replace 126 with the ID of the field to populate
   $posts = get_posts( array('post_type' => 'product', 'product_cat' => 'PRODUCT-CATEGORY-SLUG', 'post_status' => array('publish', 'private'), 'numberposts' => 999, 'orderby' => 'title', 'order' => 'ASC'));
   unset($values['options']);

   foreach($posts as $p){
     $values['options'][$p->ID] = $p->post_title;
   }
   $values['use_key'] = true; //this will set the field to save the post ID instead of post title
}

return $values;
}

Thank you for sharing! I've switched this to "User tips and tricks" instead of a question.

Hi, Can you explain a little more on how this cool bit of code works on a site please?

Does it add a Woo Product into a form and then allows the 'submit' button to act as the buy now or add to cart button for that product?

Will data from the other fields on same form be passed to anywhere?

-----

The reason for my question... I have a site selling online courses, my form needs to split the user type into those who are buying a 1-off lesson or those who are affiliated to the site's sponsors subscription (the form uses conditionals to display the appropriate buttons).

 

This code simply takes all products that are in any single product category and puts them into a checkbox, radio buttons, or dropdown menu. In the case above, it was a checkbox field, so a line of code had to be removed. Here is the original page where the code came from. There you will see a line you need to remove if the field is a radio button or checkbox field:

//remove this line if you are using a checkbox or radio button field

In your case, it sounds like you need different fields to appear depending on a users' role. Then, you could put your products into separate categories (i.e. subscriber-category and one-time-buyer-category). This would cause you to create duplicate products and put each product into it's specific category though.

Hope this helps,

Phil

Thanks for your helpful reply Phil, I'll try it today.

How would this code be modified to pull the data from another database on the same server?

I have our courses in WooCommerce in different subdomain and database.  So I would be pulling those courses from that database.

Hi there,

I have implemented this code on an order form using formidable and i worked. However I see that the list is picking up duplicates. I suspect it is because of the translated products. How do I show the English names on the English form field and the Dutch names on the Dutch form field please?

Regards,

Karl

Discussion closed.