0 0
Read Time:54 Second
//cambios del checkout order comments
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
add_filter( 'woocommerce_checkout_fields' , 'bbloomer_custom_order_notes' );
 
function bbloomer_custom_order_notes( $fields ) {
	$fields['billing']['new_order_notes'] = array( 'type' => 'textarea', 'placeholder' => '# Depto, Letra Casa o Indicaciones de entrega','label' => 'Información complementaria', 
   'class' => array('form-row-wide'), 'clear' => true, 'priority' => 100, );
	$fields['billing']['new_order_notes']['maxlength'] = 80;
   return $fields;
}

add_action( 'woocommerce_checkout_process', 'bbloomer_checkout_fields_custom_validation' );
   
function bbloomer_checkout_fields_custom_validation() { 
   if ( isset( $_POST['billing']['new_order_notes'] ) && ! empty( $_POST['billing']['new_order_notes'] ) ) {
      if ( strlen( $_POST['billing']['new_order_notes'] ) < 80 ) {
         wc_add_notice( 'Máximo 150 caracteres', 'error' );
      }
   }   
}

add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_custom_field_value_to_order_notes', 10, 2 );
 
function bbloomer_custom_field_value_to_order_notes( $order_id, $data ) {
   if ( ! is_object( $order_id ) ) { $order = wc_get_order( $order_id ); }
   $order->set_customer_note( isset( $data['new_order_notes'] ) ? $data['new_order_notes'] : '' );
   wc_create_order_note( $order_id, $data['new_order_notes'], true, true );
   $order->save();
}
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %