Skip Navigation

[Resolved] Building an order form

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

Last updated by Minesh 1 year, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2654365

Minesh
Supporter

Languages: English (English )

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

I've created the "order-details" repeating field group:
- hidden link

Which holds the following custom fields:
- Product ID
- Product Title
- Order Quantity
- Customer ID

To the view that displays the items - I've added "Custom HTML" which adds the Quantity box and checkbox:
- hidden link

<div style="padding:10px; background-color:#ededed;">
<p>Quantity: <input type="text" name="qty[]" class="qty_box" post-id="[wpv-post-id]" /></p>

<span> SELECT TO ORDER</span> 
<input type="checkbox" name="selected_order_ids[]" disabled="disabled" value="[wpv-post-id]" class="selected_order_ids" post-id="[wpv-post-id]" /> </div>

With the form which is added below:
- hidden link
I've added the following custom JS code to form's JS editor:

jQuery(document).ready(function($){  

   var prod_qty = '';
   $(".qty_box").on('change keyup',function(){
      var postid = $(this).attr('post-id');
     
      if( $(this).val() !== '') {	
     
        if(!$(".selected_order_ids[post-id='"+postid+"']").is(":checked"))
         	$(".selected_order_ids[post-id='"+postid+"']").prop("checked","checked");
       
        if($(".selected_order_ids[post-id='"+postid+"']").is(":disabled"))
        	$(".selected_order_ids[post-id='"+postid+"']").prop("disabled",'');
       
    	     //$(".selected_order_ids[post-id='"+postid+"']").trigger('change');
      }else{
         //$(".selected_order_ids[post-id='"+postid+"']").trigger('click');
         $(".selected_order_ids[post-id='"+postid+"']").prop("checked",'');
         $(".selected_order_ids[post-id='"+postid+"']").prop("disabled",'disabled');
        
         //$(".selected_order_ids[post-id='"+postid+"']").trigger('change');
        
     }
     
     
      var  order_qty =    $('input[class*="selected_order_ids"]:checked').map(function() { return $(this).val().toString()+"-"+$(".qty_box[post-id='"+$(this).attr('post-id')+"']").val().toString(); } ).get().join(",");
      $("#order_ids").val(order_qty);
   
   });
   
   
});

To the "Custom Code" section offered by Toolset, I've added the following code snippet that is used to add the repeating field group entries:
- hidden link

add_action('cred_save_data','func_add_repeating_field_entries',10,2);
function func_add_repeating_field_entries($post_id,$form_data) {
   global $current_user;
    if ($form_data['id']==4540) {
                   
       // setting unique order ID and slug
       $unique_id = abs( crc32( uniqid() ) );    
       $title= $unique_id. '-' .$current_user->ID;
       $args = array('ID' => $post_id, 'post_title' => $title,'post_name' => $title);
       wp_update_post($args);
     
                //// adding repeating field group for selected product_id=>Qty
      
      			$all_ids_qtys = explode(",",$_POST['js_order_ids']);
      			foreach($all_ids_qtys as $k=>$v):
      				$id_qty = explode("-",$v);
      				$prod_id = $id_qty[0];
      				$qty = $id_qty[1];
      	
      				 // Child Post Args
      					 $unique_id = abs( crc32( uniqid() ) );    
                        $child_post_args = array(
                          'post_title'  => $unique_id,
                          'post_name'  => $unique_id ,
                          'post_status'   => 'publish',
                          'post_type'   => 'order-details', // Here you can put your child post type slug
                        );
   
                        // Insert the post into the database
                        $child_post_id = wp_insert_post( $child_post_args );
      
      					if( isset( $child_post_id ) && $child_post_id ) {
                                           
                           update_post_meta($child_post_id, 'wpcf-order-product-id',$prod_id);
                          
                           $product_title =	get_the_title($prod_id);
                           update_post_meta($child_post_id, 'wpcf-order-product-title',$product_title);
                           update_post_meta($child_post_id, 'wpcf-order-quantity',$qty);
                           update_post_meta($child_post_id, 'wpcf-order-customer-id',$current_user->ID);
                           
                           // connecting repeating field group post with parent order
                           toolset_connect_posts('order-details', $post_id,  $child_post_id );
                        }
      
      
      			endforeach;
      
                        
                        
    }
}

On the frontend - you should try to add quantity and order it:
- hidden link

When you add the Quantity it will automatically select the related item checkbox and the pair of item_id-quantity will be displayed with the textbox of order form.

More info:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/
- https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This is the maximum help I can offer you at the moment. I hope this will help you serve your client needs and you are welcome to share your feedback.

#2655173

Minesh
Supporter

Languages: English (English )

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

Have you able to go through the solution I shared?

#2655217

Hello Minesh,
I took a quick look last night and I thought I was having issues with my browser because here's what happens: in the page you provided (hidden link) I am able to add quantity, but the select box is grayed out or not available for checking, and I see no button to submit the "order". I changed browser, but issue still exists.
Am I doing something wrong?

#2655745

Minesh
Supporter

Languages: English (English )

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

Can you please make sure you are loggedin to the site and then check.

Here is a sandbox auto-login link:
- hidden link

#2655779

Ok, I was expecting to see product ids in the order_ids field but of course the information was moved to the repeating group field.
Now I would need a little time to study it, is the sandbox website going to stay online as long as I need it or is it an expiring website?
Thank you

#2655783

Minesh
Supporter

Languages: English (English )

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

Ok - the site will be still available for couple of weeks. You can review it.

#2662957

In the end the client opted for a different solution installing woocommerce and just turning the cart into an order form. I saved the code provided by you tho, because it's a valuable example that can be useful in some other situation. Thank you!