Custom Conditional Checkout Field
-
I have made a conditional field on woocommerce checkout that lets the user insert date and that data is transmitted to admin,customer emails, thankyou pages and order meta. The field is required when it is shown.
Now i am making a condition where the field will be visible if and only if a certain specified category exist in cart. While i have succeeded in making the field appear only on the given condition, the validation is not working. It does show up red when empty but when we Place the order, the order is published but without the date. This problem did not exist when used outside the category check, where the empty field was noted and checkout process was halted.
The following is the code
function custom_woocommerce_billing_fields($fields) { $fields['billing']['billing_pickup_date'] = array( 'label' => __('Pickup Date', 'woocommerce'), // Add custom field label 'placeholder' => _x('Select Pickup Date', 'placeholder', 'woocommerce'), // Add custom field placeholder 'required' => true, // if field is required or not 'clear' => false, // add clear or not 'type' => 'text', // add field type 'class' => array('add_delivery_date'), // add class name 'id' =>'datepicker', ); return $fields; } function my_custom_checkout_field_process() { if(isset($_POST['billing_pickup-date'])){ if ( empty($_POST['billing_pickup_date']) ) wc_add_notice( __( 'Please enter a value.' ), 'error' ); } } function my_custom_checkout_field_update_order_meta( $order_id ) { if ( ! empty( $_POST['billing_pickup_date'] ) ) { update_post_meta( $order_id, 'billing_pickup_date', sanitize_text_field( $_POST['billing_pickup_date'] ) ); } } function my_custom_checkout_field_display_admin_order_meta($order){ echo '<p><strong>'.__('Pickup Date').':</strong> <br/>' . get_post_meta( $order->get_id(), 'billing_pickup_date', true ) . '</p>'; } function my_custom_checkout_field_display_thank_you_page($order){ echo '<p><strong>'.__('Pickup Date').':</strong> <br/>' . get_post_meta( $order->get_id(), 'billing_pickup_date', true ) . '</p>'; } function show_new_checkout_field_emails( $order, $sent_to_admin, $plain_text, $email ) { if ( get_post_meta( $order->get_id(), 'billing_pickup_date', true ) ) echo '<p><strong>Requested Pickup Date :</strong> ' . get_post_meta( $order->get_id(), 'billing_pickup_date', true ) . '</p>'; } function add_the_new_functionalities() { $category = 'catering'; $product_cat = get_term_by( 'slug', $category, 'product_cat' ); if ( is_category_in_cart( $category ) ) { wp_enqueue_script( 'jquery-ui-datepicker' ); // You need styling for the datepicker. For simplicity I've linked to Google's hosted jQuery UI CSS. wp_register_style( 'jquery-ui', '//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css' ); wp_enqueue_style( 'jquery-ui' ); add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields'); //validates the datepicker add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); //sends value to order meta add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' ); //display on thank you page add_action( 'woocommerce_order_details_after_order_table', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); //dispalys on order meta/ add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_thank_you_page', 10, 1 ); //send email to administrator add_action( 'woocommerce_email_after_order_table', 'show_new_checkout_field_emails', 20, 4 ); } } add_action( 'woocommerce_check_cart_items', 'add_the_new_functionalities' ); function is_category_in_cart( $category ) { foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { if ( has_term( $category, 'product_cat', $cart_item['data']->id ) ) { return true; } } return false; }
The page I need help with: [log in to see the link]
- The topic ‘Custom Conditional Checkout Field’ is closed to new replies.