I am trying to auto-populate Woocommerce product categories into a dropdown form field. This code worked when I was trialling it with WordPress categories but now doesn’t populate the Woocommerce ones. I used “post” as “type” and “categories” as “taxonomy” when trialling with WordPress post.

add_filter('frm_setup_new_fields_vars', 'frm_populate_int_categories', 20, 2);
add_filter('frm_setup_edit_fields_vars', 'frm_populate_int_categories', 20, 2);
function frm_populate_int_categories( $values, $field ) {
    if ( $field->id == 683 ) { //replace 125 with the ID of the field to populate

    // Adjust your category aruments as needed
    $category_args = array(
        'orderby' => 'name',
        'order' => 'ASC',
        'hide_empty' => false,
        'exclude' => array(232, 277),
        'type' => 'product',
        'taxonomy' => 'product_cat',
    );

    $categories = get_categories( $category_args );

    unset( $values['options'] );
    $values['options'] = array( '' ); //remove this line if you are using a checkbox or radio field

    foreach( $categories as $category ){
        $values['options'][ $category->term_id ] = $category->name;
    }

    $values['use_key'] = true; //this will set the field to save the category ID
    }

    return $values;
}

I have changed “type to “product” and “taxonomy” to “product_cat” and it still doesn’t seem to be working.

Read more here: Populating Woocommerce Categories


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: