I have a post form were I can purchase 2 different products.
Hi, can you tell me more about how you set this up? Please share any code from the Form editor here for me to review. If you have used any custom PHP code with the Forms Commerce API, please share that code here as well
The form is for create a Custom Post Type. There is a field where you can choose the product to buy by "The form specifies the product according to the value of this custom field: " "field 1".
The fields of the form are numerical or text field except the field where you can select the product that is
<div class="form-group">
<label>Field 1</label>
[cred_field field='field1' post='CPT1' value='' urlparam='' select_text='--- not set ---' class='form-control' output='bootstrap']
</div>
This form have some custom code related to the hooks "cred_form_validate" and "cred_save_data" but not related to the Forms Commerce API.
I show the code:
add_action('cred_save_data', 'save_first_CPT1',10,2);
function save_first_CPT1($post_id, $form_data){
if($form_data['id']==1248){
//Get CPT1 data
$n_CPT1=get_the_title( $post_id);
$nu_CPT1=get_post_meta( $post_id, "wpcf_old_num-de-CPT1-ja-pagat", true );
$met_CPT1=get_post_meta( $post_id, "wpcf-metode-de-pagament-CPT1", true );
//Si CPT1 exsitent
if($met_CPT1==74){
if(isset($$nu_CPT1)){
$id_CPT1_vell_a_eli=0;
//Buscar id post antic
// The Query
$argum = array('post_type' => 'CPT1-antic');
$consult = new WP_Query( $argum );
// The Loop
if ( $consult->have_posts() ) {
while ( $consult->have_posts() ) {
$consult->the_post();
$CPT1_ide=get_the_ID();
$date_CPT1=get_post_meta( $CPT1_ide,'wpcf_old_naix',true );
$old_num_CPT1=get_post_meta( $CPT1_ide,'wpcf_old_num-CPT1-antic',true );
$old_name_CPT1=get_the_title();
if(((strcmp($n_CPT1, $old_name_CPT1))==0)&&($old_num_CPT1==$$nu_CPT1)){
$id_CPT1_vell_a_eli=$CPT1_ide;
}
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo "NO posts";
}
//Delete post CPT1 antic
if($id_CPT1_vell_a_eli!=0){
wp_trash_post($id_CPT1_vell_a_eli);
}
}
}else{
if($met_CPT1==32){
//Assignar num CPT1
//Buscar el número CPT1 antic més alt
$argume = array('post_type' => 'CPT1-antic');
$conslt = new WP_Query( $argume );
// The Loop
$old_num_CPT1_temp=0;
$old_num_CPT1_max=0;
if ( $conslt->have_posts() ) {
while ( $conslt->have_posts() ) {
$conslt->the_post();
$CPT1_iden=get_the_ID();
$old_num_CPT1_temp=get_post_meta( $CPT1_iden,'wpcf_old_num-CPT1-antic',true );
if($old_num_CPT1_temp>$old_num_CPT1_max){
$old_num_CPT1_max=$old_num_CPT1_temp;
}
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo "No CPT1s old";
}
//Recorrer el número de CPT1 existent més alt
$argume = array('post_type' => 'CPT1cdp');
$conslt = new WP_Query( $argume );
// The Loop
$num_cpt1_nou_temp=0;
$num_cpt1_nou_max=0;
if ( $conslt->have_posts() ) {
while ( $conslt->have_posts() ) {
$conslt->the_post();
$CPT1_ident=get_the_ID();
$num_cpt1_nou_temp=get_post_meta( $CPT1_ident,'wpcf_old_num-de-CPT1-ja-pagat',true );
if($num_cpt1_nou_temp>$num_cpt1_nou_max){
$num_cpt1_nou_max=$num_cpt1_nou_temp;
}
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
echo "No CPT1s news";
}
if($num_cpt1_nou_max>$old_num_CPT1_max){
$num_cpt1_sav=$num_cpt1_nou_max+1;
}
else{
echo "something wrong";
$num_cpt1_sav=999999999999;
}
}
}
//Save CPT1
$elmeur_post = array(
'ID' => $post_id,
'post_title' =>$n_CPT1);
if(isset($post_id,$n_CPT1)){
wp_update_post( $elmeur_post);
}
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo $error;
}
}
if(isset($num_cpt1_sav)){
update_post_meta($post_id, 'wpcf_old_num-de-CPT1-ja-pagat', $num_cpt1_sav);
}
}
}
After payment one product change his status to "published" but the other has the status of "draft"
Are you trying to change the status of the Product that was purchased, or of the post created/edited by Forms? What type of post is being created or edited by this Form?
I'm trying to change the status of the post created by the form. I hope that the status change to "published" when the order related to products added to the cart in this form change to "completed".
Thanks