Skip Navigation

[Resolved] Auto populate product name with custom field

This support ticket is created 7 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
- 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 4 replies, has 2 voices.

Last updated by johannD-2 7 years ago.

Assisted by: Minesh.

Author
Posts
#594072

Like this ticket : https://toolset.com/forums/topic/cred-form-title-auto-fill/

I create a post form and I want the product name populate with product category and a custom field (ville-d-arrivee) within the CRED Post form.

So i tried this code :

add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
    // if a specific form
    if ($form_data['id']==763) {
        if(!empty($_POST['wpcf-product_cat'])){
            $depart = $_POST['wpcf-product_cat'];
        }
        if(!empty($_POST['wpcf-ville-d-arrivee'])){
            $arrivee = $_POST['wpcf-ville-d-arrivee'];
        }
        $title = $depart .' '. $arrive;
        $slug = sanitize_title($title);
        wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
    }
}

Here a bit of my CRED form


[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']

<div class="row">
	<div class="col-sm-6">
      <b><p>Départ</p></b>
		[cred_field field='product_cat' display='select' single_select='true' output='bootstrap']
	</div>
	<div class="col-sm-6">
      <b><p>Arrivée</p></b>
		[cred_field field='ville-d-arrivee' display='select' single_select='true' output='bootstrap']
  	</div>
</div>

Infortunately this is not working.

Could you help me ?

#594228

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - you write almost proper code but to get the selected product category name you need to use wp_get_post_terms() function.

So, your code should look like:

add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){

 $title =   get_the_title($post_id);

    // if a specific form
    if ($form_data['id']==763) {
        if(!empty($_POST['product_cat'])){
            $term_list = wp_get_post_terms($post_id, 'product_cat', array("fields" => "names"));
            $depart = join("-",$term_list);
        }

        if(!empty($_POST['wpcf-ville-d-arrivee'])){
            $arrive = $_POST['wpcf-ville-d-arrivee'];
        }
        $title = $title.' '.$depart .' '. $arrive;
        $slug = sanitize_title($title);
        wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
    }
}

I've adjusted the code as you can see above, could you please confirm it works for you as well.

#594364

i used wp_get_post_terms() for the second field too cause it's a custom taxonomy and it's works well

I tried to add the custom field " date-de-depart" but it's not working.

add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
 
 $title =   get_the_title($post_id);
 
    // if a specific form
    if ($form_data['id']==763) {
        if(!empty($_POST['product_cat'])){
            $term_list = wp_get_post_terms($post_id, 'product_cat', array("fields" => "names"));
            $depart = join("-",$term_list);
        }
 
        if(!empty($_POST['product_cat'])){
            $term_list = wp_get_post_terms($post_id, 'ville-d-arrivee', array("fields" => "names"));
            $arrivee = join("-",$term_list);
        }
        if(!empty($_POST['wpcf-date-de-depart'])){
            $date = $_POST['wpcf-date-de-depart'];
        }
        
        $title = $depart .' => '.$arrivee . ' le '. $date;
        $slug = sanitize_title($title);
        wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
    }
}
#594370

Minesh
Supporter

Languages: English (English )

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

is "wpcf-date-de-depart" is a Types date field? If Yes, Types date field stores its value as timestamp.

You need to covert timestamp to date. Please try to use following code:

add_action('cred_save_data', 'my_custom_save_data_action', 10, 2);
function my_custom_save_data_action( $post_id, $form_data ){
  
 $title =   get_the_title($post_id);
  
    // if a specific form
    if ($form_data['id']==763) {
        if(!empty($_POST['product_cat'])){
            $term_list = wp_get_post_terms($post_id, 'product_cat', array("fields" => "names"));
            $depart = join("-",$term_list);
        }
  
        if(!empty($_POST['product_cat'])){
            $term_list = wp_get_post_terms($post_id, 'ville-d-arrivee', array("fields" => "names"));
            $arrivee = join("-",$term_list);
        }
        if(!empty($_POST['wpcf-date-de-depart'])){
            $date = get_post_meta($post_id,'wpcf-date-de-depart',true);
             $date = date('Y-m-d',$date);
        }
         
        $title = $depart .' => '.$arrivee . ' le '. $date;
        $slug = sanitize_title($title);
        wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
    }
}

You can change the date format as per your requirement. As you can see I've setup 'Y-m-d' format for now.

Could you please confirm it works as your end as well.

#594390

I will adjust the date format but works fine !! Thanks you. 😉