Skip to content

Instantly share code, notes, and snippets.

@ScottDeLuzio
Last active November 20, 2018 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottDeLuzio/700b10aeb66e47a176989b55c48a42b6 to your computer and use it in GitHub Desktop.
Save ScottDeLuzio/700b10aeb66e47a176989b55c48a42b6 to your computer and use it in GitHub Desktop.
Save empty conditional fields with order in Conditional Woo Checkout Field Pro
// Add this to your theme's functions.php file or a custom plugin.
add_action( 'woocommerce_checkout_update_order_meta', 'conditional_checkout_field_pro_update_order_meta_blank_entries_only' );
function conditional_checkout_field_pro_update_order_meta_blank_entries_only( $order_id ) {
global $wpdb, $cwcfp_db_table;
$fields = $wpdb->get_results("SELECT * FROM " . $cwcfp_db_table . " ORDER BY id;");
//Check to see if the checkout has been processed already or not
$did_process = get_post_meta( $order_id, '_cwcfp_did_process', true );
switch ( $did_process ) {
case 'processed':
foreach ( $fields as $field ) {
// The order was processed already, but did not complete, so we are deleting the old information in case it changed.
delete_post_meta( $order_id, esc_html( $field->field_title ) );
$id = esc_html( $field->id );
$quantity = cwcfp_get_field_quantity( $id );
for( $b=1; $b <= $quantity; $b++ ){
$num_to_skip = 0;
$skip = (int) apply_filters( 'cwcfp_skip_first', $num_to_skip, $id );
if( $b > $skip ){
if ( isset( $_POST[ 'conditional_field_' . $id . '-' . $b ] ) && '' == trim( $_POST[ 'conditional_field_' . $id . '-' . $b ] ) ) {
// Now we'll add the new information from the customer.
add_post_meta( $order_id, esc_html( $field->field_title ), sanitize_text_field( $_POST[ 'conditional_field_' . $id . '-' . $b ] ) );
}
}
}
}
// Indicates that the order has been processed
update_post_meta( $order_id, '_cwcfp_did_process', 'processed' );
break;
default:
foreach ( $fields as $field ) {
$id = esc_html( $field->id );
$quantity = cwcfp_get_field_quantity( $id );
for( $b=1; $b <= $quantity; $b++ ){
$num_to_skip = 0;
$skip = (int) apply_filters( 'cwcfp_skip_first', $num_to_skip, $id );
if( $b > $skip ){
if ( isset( $_POST[ 'conditional_field_' . $id . '-' . $b ] ) && '' == trim( $_POST[ 'conditional_field_' . $id . '-' . $b ] ) ) {
add_post_meta( $order_id, esc_html( $field->field_title ), sanitize_text_field( $_POST[ 'conditional_field_' . $id . '-' . $b ] ) );
}
}
}
}
// Indicates that the order has been processed
update_post_meta( $order_id, '_cwcfp_did_process', 'processed' );
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment