Skip Navigation

[Resolved] Split: Bug after upgrading to CRED version 1.9.4

This support ticket is created 6 years, 10 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Last updated by Christian Cox 6 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#611945

Thanks Christian. I followed all of your steps and the alt_product_cat select generic field is now visible on the frontend of the form.

#611950

Great, now add this code to functions.php to capture the selected alternate product category terms and apply them to the post:

add_action('cred_save_data', 'save_alt_product_cat',100,2);
function save_alt_product_cat($post_id, $form_data) {
  $forms = array( 123, 456 );
  if( in_array( $form_data['id'], $forms)) {
    $taxonomy = 'product_cat';
    $term = isset($_POST['alt_product_cat']) ? $_POST['alt_product_cat'] : '';
    if( $term != '' ) {
      wp_set_object_terms( $post_id, $term, $taxonomy, true );
    }
  }
}

Replace 123, 456 with a comma-separated list of form IDs where you plan to use the alt_product_cat field. Test this out and let me know the results. The term you select in the alt_product_cat select field should be applied to the post when you check it in wp-admin, as well as the term you chose in product_cat, if any.

#611955

Works like a charm 😉 Thank you so much Christian.

Do you know how I add a default value i.e.: --Select category-- to the CRED generic alt_product_cat field and also the product_cat field?

This works for me for the product_cat field, but maybe there's a parameter I can use.

 jQuery("select[name='product_cat[]']").prepend('<option value="0">- Select a Category-</option>');
 jQuery(".select[name='product_cat[]'] option[value='0']").attr('selected','selected');
#611964

Oh before I forget. How would I also make both of these fields required?

#611991

Interesting the problem I reported in the first place didn't go away.
https://toolset.com/forums/topic/split-bug-after-upgrading-to-cred-version-1-9-4/

"I have this field [cred_field field='product_cat' display='select' single_select="true"] placed two times in a single cred edit form.
The selected category is saved to the wp backend when creating the post, however on the frontend of the CRED edit form the wrong category is selected. (Actually the first category of the dropdown box-->default category)
This doesn't happen in CRED version 1.9.3.1. "

But this time only the [cred_field field='product_cat' display='select' single_select="true"] field is affected.

The cred generic field works fine. So it's not due to the fact that I had this field [cred_field field='product_cat' display='select' single_select="true"] two times in one CRED form. Hm...

I am going back to version 1.9.3.1 again, as it doesn't happen there. Uugh.

#612012

Do you know how I add a default value i.e.: --Select category-- to the CRED generic alt_product_cat field and also the product_cat field?
In the alt_product_cat field, just add an option to the data payload with the label "--Select category--" and the value "0". I don't think there is a way to force an empty option on a taxonomy field in CRED. Anyway when editing a post at least one term will always be selected - Uncategorized. Even if you deselect all terms for a post in wp-admin, Uncategorized will be automatically added.

The selected category is saved to the wp backend when creating the post, however on the frontend of the CRED edit form the wrong category is selected.
Well if more than one term is selected, a single select field will not work correctly because two options will have the "selected" flag. Can I log in and see what you're noticing in CRED 1.9.3.1 that is not working in CRED 1.9.4? Please tell me which post you are editing, and how I can find the CRED form on the frontend.

Oh before I forget. How would I also make both of these fields required?
On the front-end you can make a generic field required by setting "required" to 1 in the data payload. There is no option to require someone to select a taxonomy term in a CRED field on the front-end, but you can validate both of these on the backend with cred_form_validate. We haven't gotten to that yet, hold on.

#612516

I quickly set this up for you on an old test site, where CRED 1.9.3.1. is active and the old approach still works so you can see the bug when updating.
It doesn't seem to work exactly like you're describing here either. I created this product:
hidden link
I selected the categories "new" (first field) and "adsfasdf" (second field) for this product when I created it. When I edit the product here:
hidden link
...both select fields show "adsfasdf" selected, which is not correct. What am I missing?

Also do you know how I can make this work so that when I use the product_cat and the generic alt_product_cat field and select different categories there, the right category, which I choose beforehand with the product_cat and the generic alt_product_cat will be selected in the CRED edit form and not just only one?
I've been trying to explain that it is not possible to know which term was chosen for either box after the post is saved because the selected terms are not stored individually. This is evident in the example I provided above with adsfasdf selected twice when editing a post. The only way to know which was selected in each box is to use another custom field or fields to store the selected values individually for each box. If you want to be able to know which term was stored in each box like this, my recommendation is to use two different custom fields. Do not use the taxonomy term cred_field at all in this form, because as you can see it is not designed to work when more than one term can be saved with the post. If the product_cat term association with the product is necessary for other reasons on your site, use custom code to manipulate the term associations using cred_save_data.

#612519

Hello Christian.
Sorry for the misunderstanding. The way you just explained it and experienced it is exactly how it works at the moment.
I understand that this is not possible with the standard product_cat and that I need custom fields to achieve that.

You said the following: "If the product_cat term association with the product is necessary for other reasons on your site, use custom code to manipulate the term associations using cred_save_data."
What do you mean by "manipulate the term associations"?

Still, do you mind showing me how I can validate the two product_cat and alt_product_cat fields in order to make them required? That's always something I wanted to do/know.

#612565

What do you mean by "manipulate the term associations"?
I mean that the Product post created by CRED will not have any product_cat terms associated with it when you use two custom fields instead of the CRED product_cat field. Those selected categories will be saved only as custom field values. So if you want to apply "new" and "adsfasdf" to the Product as product_cat terms, you must update the post's terms using the WordPress API, for example with wp_set_object_terms (https://codex.wordpress.org/Function_Reference/wp_set_object_terms). Remove any previously associated product_cat terms, and apply the new terms based on the selections in the custom fields. Something like this:

add_action('cred_save_data', 'set_product_cats_from_custom_fields',100,2);
function set_product_cats_from_custom_fields($post_id, $form_data) {
  $forms = array( 12345,67890 );
  if( in_array( $form_data['id'], $forms ) ) {
    $taxonomy = 'product_cat';
    // make an array using the values from the generic fields, then modify the posts's term associations
    $term1 = isset($_POST['generic-field-a']) ? $_POST['generic-field-a'] : null;
    $term2 = isset($_POST['generic-field-b']) ? $_POST['generic-field-b'] : null;
    $tags = array( $term1, $term2 );
    wp_set_object_terms( $post_id, $tags, $taxonomy, false );
  }
}

Still, do you mind showing me how I can validate the two product_cat and alt_product_cat fields in order to make them required?
Here's a basic validation example that tests to see if any product_cat terms are selected in a standard product_cat CRED field, and returns an error if not:

add_filter('cred_form_validate','validate_product_cat_and_alt_cat',10,2);
function validate_product_cat_and_alt_cat($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    $forms = array( 12345 );
    if( in_array( $form_data['id'], $forms ) ) {
        //check selected product_cats
        if ($fields['product_cat']['value']==null)
        {
            //set error message for my_field
            $errors['product_cat']='You must select at least one Product Category';
        }
    }
    //return result
    return array($fields,$errors);
}

More information about the validation hook here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#612659

Thank you Christian.
So validation the generic field was pretty easy. I just set required to 1.

But validating the product_cat terms with your code snippet didn't work for me for some reason.

#612881

You may need to make modifications to fit your needs. Did you replace 12345 with your form ID?

#612886

Yes.

Maybe my jQuery code might be interfering with it. I added an option with the label of --Select Category-- with the no value to the select fields.

 //select taxonomy 
       jQuery('.page-campaign-form select[name="product_cat[]"], .page-campaign-form-edit select[name="product_cat[]"], .page-campaign-form-edit select[name="alt_product_cat"]').prepend('<option value="">- Select a Category-</option>');
	
jQuery('.page-campaign-form select[name="alt_product_cat"] option[value=""], .page-campaign-form-edit select[name="alt_product_cat"] option[value=""]').remove();
jQuery('.page-campaign-form select[name="alt_product_cat"], .page-campaign-form-edit select[name="alt_product_cat"]').prepend('<option value="">- Select a Category-</option>');
	
jQuery('.page-campaign-form select[name="product_cat[]"] option[value=""], .page-campaign-form select[name="alt_product_cat"] option[value=""], .page-campaign-form-edit select[name="product_cat[]"] option[value=""], .page-campaign-form-edit select[name="alt_product_cat"] option[value=""]').attr('selected','selected');
	
#612951

Let's add some PHP logs to see what's happening. Here's how you can do that:

add_filter('cred_form_validate','validate_product_cat_and_alt_cat',10,2);
function validate_product_cat_and_alt_cat($error_fields, $form_data)
{
error_log('cred_form_validate product cat');
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    $forms = array( 12345 );
    if( in_array( $form_data['id'], $forms ) ) {
error_log('in array');
error_log(print_r($fields, true));
error_log('is empty string? ' . $fields['product_cat']['value'] == '');
error_log('is null? ' . $fields['product_cat']['value'] == null);
        //check selected product_cats
        if ($fields['product_cat']['value']==null)
        {
error_log('conditional true');
            //set error message for my_field
            $errors['product_cat']='You must select at least one Product Category';
        }
    }
    //return result
    return array($fields,$errors);
}

Replace 12345 again, then turn on error logging temporarily. If you're not familiar with those logs, I can show you how to activate them. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

Submit the CRED form again. This should create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the changes you made to wp-config.php.