Support » Plugin: Delivery Date System for WooCommerce » Make delivery time field not required

  • Dear friends at Haste,

    Is there a way to make delivery time field not required? I’m trying to achieve it with this code, but it doesn’t work:

    
    function make_delivery_time_not_required( $address_fields ) {
         $address_fields['delivery_time']['required'] = false;
         return $address_fields;
    }
    add_filter( 'woocommerce_checkout_fields' , 'make_delivery_time_not_required' );
    

    Thanks in advance,
    Marco Andrei

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Allyson Souza

    (@allysonsouza)

    Hi Marco,

    The easiest way I though you could do that is unregistering the hook where we create the fields in the plugin, and then recreating it without the required option, like that:

    
    remove_action( 'woocommerce_before_order_notes', 'delivery_date_system_echo_fields', 5 );
    
    function delivery_date_system_echo_non_required_fields( $checkout ) {
    	echo '<div class="delivery-options">';
    	
    	woocommerce_form_field( 'delivery_date', array(
    		'type'          => 'text',
    		'class'         => array('form-row-wide'),
    		'id'            => 'datepicker',
    		'required'      => false,
    		'label'         => __( 'Select one of the available delivery days', 'delivery-date-system' ),
    		'placeholder'   => __( 'Open calendar', 'delivery-date-system' ),
    		'autocomplete'  => 'off',
    	));
    	
    	if ( ! empty( delivery_time_options() ) ) {
    		woocommerce_form_field( 'delivery_time', array(
    			'type'          => 'select',
    			'class'         => array('form-row-wide'),
    			'id'            => 'delivery-time',
    			'required'      => false,
    			'label'         => __( 'Select a delivery time', 'delivery-date-system' ),
    			'options'     	=> delivery_time_options()
    		));
    	}
    		
    	echo '</div>';
    }
    add_action( 'woocommerce_before_order_notes', 'delivery_date_system_echo_non_required_fields', 5 );
    

    I hope this can help you!

    Best regards,
    Allyson Souza

    Plugin Contributor Allyson Souza

    (@allysonsouza)

    Hi @marcoandrei, it’s been a while since we get from you, any updates on this issue? The snippet helped you?

    Thanks!

    ola Allison,
    Como posso desativar o calendário, só quero a opção de escolher horário

    desde já agradeço.

    Plugin Contributor Allyson Souza

    (@allysonsouza)

    Olá @kensuke3k,

    Infelizmente no momento não há opções para desativar o calendário, mas é um ótima ideia para uma versão futura. Se você tiver conhecimentos de programação, é possível remover os hooks que registram e validam os campos e recria-los em seu functions.php sem campo de data:

    function delivery_date_system_echo_fields_without_calendar( $checkout ) {
    	echo '<div class="delivery-options">';
    	
    	if ( ! empty( delivery_time_options() ) ) {
    		woocommerce_form_field( 'delivery_time', array(
    			'type'          => 'select',
    			'class'         => array('form-row-wide'),
    			'id'            => 'delivery-time',
    			'required'      => true,
    			'label'         => __( 'Select a delivery time', 'delivery-date-system' ),
    			'options'     	=> delivery_time_options()
    		));
    	}
    		
    	echo '</div>';
    }
    remove_action( 'woocommerce_before_order_notes', 'delivery_date_system_echo_fields', 5 );
    add_action( 'woocommerce_before_order_notes', 'delivery_date_system_echo_fields_without_calendar', 5 );
    
    function delivery_date_system_validate_new_checkout_fields_without_calendar() {
    	if ( empty( $_POST['delivery_time'] ) ) wc_add_notice( __( 'Select an available delivery time.', 'delivery-date-system' ), 'error' );
    }
    remove_action( 'woocommerce_checkout_process', 'delivery_date_system_validate_new_checkout_fields' );
    add_action( 'woocommerce_checkout_process', 'delivery_date_system_validate_new_checkout_fields_without_calendar' );

    Agradeço a compreensão

    Olá @allysonsouza
    funciono, mto obrigado.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.