Last active
November 20, 2018 16:31
-
-
Save ScottDeLuzio/700b10aeb66e47a176989b55c48a42b6 to your computer and use it in GitHub Desktop.
Save empty conditional fields with order in Conditional Woo Checkout Field Pro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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