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.